Java 在JUnit中使用@RunWith有解决方法吗?
我有一个主测试类(A)和另一个扩展自它的测试类(B) A有一个Java 在JUnit中使用@RunWith有解决方法吗?,java,spring,unit-testing,junit,Java,Spring,Unit Testing,Junit,我有一个主测试类(A)和另一个扩展自它的测试类(B) A有一个@RunWith(SpringJUnit4ClassRunner.class)注释来初始化它。 我需要在B上使用@RunWith(JUnitParamsRunner.class)注释 但我似乎不能同时做到这两件事。有没有一种方法可以使参数化测试套件不带@RunWith注释 另一个解决方案是使用 testContextManager = new TestContextManager(AbstractInvoiceCreationModu
@RunWith(SpringJUnit4ClassRunner.class)
注释来初始化它。
我需要在B上使用@RunWith(JUnitParamsRunner.class)
注释
但我似乎不能同时做到这两件事。有没有一种方法可以使参数化测试套件不带@RunWith
注释
另一个解决方案是使用
testContextManager = new TestContextManager(AbstractInvoiceCreationModuleTest.class);
testContextManager.prepareTestInstance(this);
但是我不能在@BeforeClass
方法中写这个,因为它是静态的,不允许使用这个。我不希望在@Before
方法中编写上述代码,因为类将在每个测试方法之前初始化。很多测试类都是从a扩展而来的
可以做些什么?我不知道这个特定的案例(Spring测试运行程序),但我有一个类似的案例,并最终创建了自己的运行程序。您可以使用规则而不是
SpringJUnit4ClassRunner。class
:Spring在版本4.2中为Spring上下文添加了对基于@rule的JUnit支持
该文件中的示例:
// Optionally specify a non-Spring Runner via @RunWith(...)
@ContextConfiguration
public class IntegrationTest {
@ClassRule
public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();
@Rule
public final SpringMethodRule springMethodRule = new SpringMethodRule();
@Test
public void testMethod() {
// execute test logic...
}
}
我不想写跑步者。这违背了使用参数化测试的目的,我将不得不编写逻辑,这可能会引入错误。我建议将SpringJunit4ClassRunner扩展到SpringJunit4ParamRunner(或反向)。显然,您不想创建一个具有特定于测试的逻辑的运行程序。不过,编写它们很痛苦,所以只有在您有许多相同模式的测试时才值得。该链接非常有用,谢谢!就我的2c而言,在每个测试方法之前初始化Spring应用程序上下文(如博客文章中所示)不是非常有效,并且会显著降低测试的性能。测试上下文应该被缓存(以某种方式)。例如,spring测试框架只初始化一次上下文,并且它在所有测试中都是共享的。