【问题标题】:Weird context behavior in tests测试中奇怪的上下文行为
【发布时间】: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...
    }

}

【问题讨论】:

    标签: java spring maven junit


    【解决方案1】:

    这是我发现的:

    SpringJUnit4ClassRunner 不兼容多线程模式

    https://jira.spring.io/browse/SPR-12421

    我通过为 sunfire 设置 parallel=classes 解决了这个问题

               <plugin>
                    <groupId>org.apache.maven.plugins</groupId>
                    <artifactId>maven-surefire-plugin</artifactId>
                    <configuration>
                        <parallel>classes</parallel>
                        <reuseForks>true</reuseForks>
                        <threadCount>1</threadCount>
                    </configuration>
                </plugin>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2013-03-23
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      • 2014-02-17
      • 1970-01-01
      相关资源
      最近更新 更多