When Maven builds my project and runs the unit tests, sometimes a concurrent modification exception is thrown (approximately 1 out of 5 times it will fail, the other times it will build successfully). But when I run the tests locally as unit tests, they all pass without the exception.
In my pom.xml file I have the Surefire plugin configured as:
<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>
However the stacktrace I get back does not mention what is causing the concurrent modification exception.
I have noticed that all tests pass on the build, but for some reason Maven reprints out the result of the test that has already passed but now has test exception ConcurrentModification.
I'm not sure what is causing the test result to be reprinted, or whether for some reason the test is being rerun at the same time while the first run of the test has not been completed since it is a parallel build? (Not sure why it would rerun a test)
Stacktrace
[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
I am not sure what is causing the Concurrent Modification Exception or how to avoid this issue from occurring as it only happens sometimes, but it is the same test that fails and not other tests in my test suite.