【发布时间】:2016-07-19 19:32:41
【问题描述】:
我正在尝试在我的测试用例中使用 SpringJUnit4ClassRunner。在只有一种 @Test 方法的测试中,它可以完美运行。 但是在使用几种@Test 方法的测试中,有时它会起作用,有时我会得到 NPE 或 IllegalStateException。 我在没有 @DirtiesContext 注释和不同的 classMode 模式的情况下尝试了它。
只有在我构建 maven 项目时才会发生这种情况。当我从 IDE 运行此测试时 - 一切正常。
附:我的上下文是无状态的。
@RunWith(SpringJUnit4ClassRunner.class)
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD)
@ContextConfiguration(classes = {TestConfiguration.class})
@ActiveProfiles("dummy")
public class TestFieldMapping {
@Autowired
private ApplicationContext context;
@Test
public void test1() {
context.getBean...
}
@Test
public void test2() {
context.getBean...
}
@Test
public void test3() {
context.getBean...
}
}
【问题讨论】: