Java: test methods of different parameter ordering

2019-09-08 14:58发布

I want to correctly validate a method regardless of the parameter ordering.

Therefore, snippet 1 and 2 should both pass the test case in snippet 3.

(snippet 1)

public Integer compute_speed(Integer distance, Integer time) {  
    return distance/time;
}

(snippet 2)

public Integer compute_speed(Integer time, Integer distance) {  
    return distance/time;
}

You can assume the two snippets as different code submissions by two students. And you can assume that the number of parameters can be as large as 10.

In the test case, I wrote,

(snippet 3)

 return compute_speed(3, 1).equals(3);

This validates snippet 1 but fails for 2. How can I make it such that both snippets pass the test case?

If only there was something like,

return compute_speed(distance = 3, time = 1).equals(3);

Thanks in advance...

2条回答
别忘想泡老子
2楼-- · 2019-09-08 15:16

I don't think you can have both test cases pass. Generally, a/b will not equal b/a.

In fact, assuming snippets 1 and 2 are in the same class, it won't even compile, because both methods have the same signature. How would Java choose which method to invoke, when both are equally valid?

I don't see any reason to have more than one method here. That's all that is needed to compute the speed.

Java doesn't have the feature of named parameter notation that, say, PL/SQL has:

speed := compute_speed(distance => 3, time => 1);

UPDATE

With the question being updated to testing different classes from different students, I would have a test case like this:

assertTrue(compute_speed(3, 1).equals(3) || compute_speed(1, 3).equals(3));

That would cover both cases of parameter ordering.

查看更多
forever°为你锁心
3楼-- · 2019-09-08 15:26

A cleaner way would be to create an interface with a Integer computeSpeed(Integer time, Integer distance); method:

public interface DistanceCalculator {
    Integer computeSpeed(Integer distance, Integer time);
}

You then ask the students to implement it and call their implementation StudentNameDistanceCalculator. For example you will receive the following classes from your students:

public class AssyliasDistanceCalculator implements DistanceCalculator {
    public Integer computeSpeed(Integer distance, Integer time) {  
        return distance / time;
    }
}

public class BobDistanceCalculator implements DistanceCalculator {
    public Integer computeSpeed(Integer distance, Integer time) {
        return distance / time * 2;
    }
}

You can then load all their classes in one project and test all the classes at once. For example with TestNg:

@Test(dataProvider = "students")
public void testMethod(Class<?> clazz) throws Exception {
    DistanceCalculator dc = (DistanceCalculator) clazz.newInstance();
    assertEquals(dc.computeSpeed(3, 1), (Integer) 3, 
            clazz.getSimpleName().replace("DistanceCalculator", "") + " failed");
}

@DataProvider(name = "students")
public Object[][] dataProvider() {
    return new Object[][]{
        {AssyliasDistanceCalculator.class},
        {BobDistanceCalculator.class}};
}

And you will get a detailed report of who doesn't pass the test:

enter image description here

查看更多
登录 后发表回答