使用 JUnit4,我想做的是能够测试一组不同的 java 项目,它们都做同样的事情,而不是必须写一个测试用例来测试每个项目,我想知道是否可能编写一个可以在多个类上运行的测试?
如果使用 JUnit4 无法做到这一点,是否可以通过其他方式做到这一点?
我知道这是不对的,但这只是为了简要说明我的意思:
@Test
public void test(Class insertClassNameHere, Method nameOfMethod){
Class insertClassNameHere = new Class();
assertEquals(insertClassNameHere.nameOfMethod(),1);
}
最佳答案
您可以使用 JUnit 的@Parameterized。
@RunWith(Parameterized.class)
public class BehaviorTest {
@Parameters
public static Collection<Object[]> classesAndMethods() {
List<Object[]> list = new ArrayList<Object[]>();
list.add(new Object[]{ Foo.class, Foo.class.getMethod("foo") });
return list;
}
private Class clazz;
private Method method;
public BehaviorTest(Class clazz, Method method) {
this.clazz = clazz;
this.method = method;
}
@Test
public void testBehavior() {
// Do stuff
}
}
关于java - 是否可以通过一次测试测试多个类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11778384/