更新时间:2023-02-14 16:55:32
更简洁的方法是使用 Integer computeSpeed(Integer time, Integer distance);
方法创建接口:
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);
}
然后您要求学生实现它并调用他们的实现 StudentNameDistanceCalculator.例如,您将收到学生的以下课程:
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;
}
}
然后您可以在一个项目中加载他们的所有类并一次测试所有类.以 TestNg 为例:
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: