how can I Unit Test toCompare() method, comparing 2 objects?

Trying to test the follwing method, with 3 scenarios. If the 2 objects are equal, return zer0, if "this" is larger than the other object return positive number, else return negative number.

Do I write 3 different tests for 3 cases? Or can I do it all in a single test method? Thanks

public int compareTo(Vehicle v){

        if(this.getLengthInFeet() == ((Boat)v).getLengthInFeet()){
            return 0;
        }else if(this.getLengthInFeet() > ((Boat)v).getLengthInFeet()){
            return 10;
        }else{
            return -10;
        }

}

Answers


Have a look at @Parameterized. This will give you on test method with multiple data points. The following is an example (untested):

@RunWith(Parameterized.class)
public class XxxTest {
    @Parameters
    public static Iterable<Object[]> data() {
        return Arrays.asList(new Object[][] {
           { 0, 10, 10 },
           { -10, 10, 20 },
        });
    }

    private final int expected;
    private final int thisFeet;
    private final int vFeet;

    public XxxTest(int expected, int thisFeet, int vFeet) {
        this.expected = expected;
        this.thisFeet = thisFeet;
        this.vFeet = vFeet;
    }

    @Test
    public void test() {
        Vehicle vThis = new Vehicle(thisFeet);
        Vehicle vThat = new Vehicle(vFeet);

        assertEquals(expected, vThis.compareTo(vThat));
    }

}

Need Your Help

How to get the full file path

php javascript

When Im using a file field and browsing an image by its full path

Ruby returning null when printing same string again

ruby git

I am modifying a pre-receive hook of gitlab