Maven Surefire并发修改异常

Maven Surefire Concurrent Modification Exception

当 Maven 构建我的项目并 运行 进行单元测试时,有时会抛出并发修改异常(大约 5 次中有 1 次会失败,其他时候会成功构建)。但是当我运行本地测试作为单元测试时,它们都无一例外地通过了。

在我的 pom.xml 文件中,我将 Surefire 插件配置为:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.19.1</version>
    <configuration>
        <forkCount>1C</forkCount>
        <reuseForks>true</reuseForks>
    </configuration>
    <dependencies>
        <dependency>
              <groupId>org.apache.maven.surefire</groupId>
              <artifactId>surefire-junit47</artifactId>
              <version>2.19.1</version>
        </dependency>
    </dependencies>
</plugin>

但是我得到的堆栈跟踪没有提到导致并发修改异常的原因。

我注意到构建时所有测试都通过了,但出于某种原因,Maven 重印了已经通过但现在有测试异常 ConcurrentModification 的测试结果。

我不确定是什么原因导致测试结果被重印,或者是否由于某种原因在测试第一个 运行 的同时重新运行还没有完成,因为它是并行构建? (不确定为什么会重新运行 测试)

堆栈跟踪

[Test executing]

13:48:24.869 [00001-main] DEBUG o.s.t.c.s.DirtiesContextTestExecutionListener - After test method: context [DefaultTestContext@375046d6 testClass = Tests, testInstance = com.application.Tests@179181c, testMethod = failingTest@Tests, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3f7b4a61 testClass = Tests, locations = '{}', classes = '{class com.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', resourceBasePath = 'src/main/app', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [false].
13:48:24.869 [00001-main] DEBUG o.s.t.c.w.ServletTestExecutionListener - Resetting RequestContextHolder for test context [DefaultTestContext@375046d6 testClass = Tests, testInstance = com.application.Tests@179181c, testMethod = failingTest@Tests, testException = [null], mergedContextConfiguration = [WebMergedContextConfiguration@3f7b4a61 testClass = Test, locations = '{}', classes = '{class com.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', resourceBasePath = 'src/main/app', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]].

[Some other tests executing]

13:48:28.632 [00001-main] DEBUG o.s.t.c.s.DirtiesContextTestExecutionListener - After test method: context [DefaultTestContext@54fde1bc testClass = Tests, testInstance = com.application.Tests@57ffa818, testMethod = failingTest@Tests, testException = java.util.ConcurrentModificationException, mergedContextConfiguration = [WebMergedContextConfiguration@5247834f testClass = Tests, locations = '{}', classes = '{class com.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', resourceBasePath = 'src/main/app', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]], class dirties context [false], class mode [null], method dirties context [false].
13:48:28.632 [00001-main] DEBUG o.s.t.c.w.ServletTestExecutionListener - Resetting RequestContextHolder for test context [DefaultTestContext@54fde1bc testClass = Tests, testInstance = com.application.Tests@57ffa818, testMethod = failingTest@Tests, testException = java.util.ConcurrentModificationException, mergedContextConfiguration = [WebMergedContextConfiguration@5247834f testClass = Tests, locations = '{}', classes = '{class com.Application}', contextInitializerClasses = '[]', activeProfiles = '{}', propertySourceLocations = '{}', propertySourceProperties = '{}', resourceBasePath = 'src/main/app', contextLoader = 'org.springframework.test.context.web.WebDelegatingSmartContextLoader', parent = [null]]].
failingTest(com.application.Tests)  Time elapsed: 0 sec  <<< ERROR!
java.util.ConcurrentModificationException

[Some other tests executing]

Results :

Tests in error: 
  Tests.failingTest» ConcurrentModification

我不确定是什么导致并发修改异常或如何避免此问题的发生,因为它有时会发生,但失败的是同一个测试,而不是我测试套件中的其他测试。

我发现问题的发生是因为一个我没有模拟的方法正在创建和 运行 一个线程(即使线程没有修改任何资源) - 所以我模拟了它问题似乎已解决。

我遇到了同样的问题,但这与 Java 8 中对 Collections.sort() 所做的更改有关。.