4

I have been running into a peculiar behavior when running a test using Spring 4.3.13 and 5.0.2 versions.

Given the following simple test, in which I am only interested on making sure I can load the context into the test:

@ContextConfiguration(classes = {MyTestConfig.class})
@RunWith(MockitoJUnitRunner.class)
public class MySpring4RunnerTest {

  @ClassRule
  public static final SpringClassRule SPRING_CLASS_RULE = new SpringClassRule();

  @Rule
  public final SpringMethodRule springMethodRule = new SpringMethodRule();

  @Autowired
  private ApplicationContext appCtx;

  @Test
  public void testBeanNames() {
    String[] beans = this.appCtx.getBeanDefinitionNames();
    Arrays.sort(beans);
    System.out.println("\nSpring Managed Beans:");
    Stream.of(beans).forEach(it -> System.out.println(it));    
  }
}

this works as expected with spring-test:4.3.13.RELEASE:

[INFO]
[INFO] Results:
[INFO]
[INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------

However if I change Spring version to 5.0.2.RELEASE (yes, including spring-test) I get the following exception message:

2018-01-17 16:37:38,128 ERROR [main] org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6f8e8894] to prepare test instance [com.example.MySpring4RunnerTest@418c5a9c] java.lang.ArrayIndexOutOfBoundsException: 1
        at org.springframework.test.context.TestExecutionListener.prepareTestInstance(TestExecutionListener.java:96)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
        at org.springframework.test.context.junit4.statements.RunPrepareTestInstanceCallbacks.evaluate(RunPrepareTestInstanceCallbacks.java:63)
        at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:84)
        at org.springframework.test.context.junit4.statements.SpringFailOnTimeout.evaluate(SpringFailOnTimeout.java:87)
        at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:103)
        at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:325)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
        at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:103)
        at org.springframework.test.context.junit4.rules.SpringClassRule$TestContextManagerCacheEvictor.evaluate(SpringClassRule.java:230)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407) 2018-01-17 16:37:38,304 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@15f47664] for test class [class com.example.MySpring4RunnerTest] java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:103)
        at org.springframework.test.context.junit4.rules.SpringClassRule$TestContextManagerCacheEvictor.evaluate(SpringClassRule.java:230)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407) 2018-01-17 16:37:38,305 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@e25951c] for test class [class com.example.MySpring4RunnerTest] java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:103)
        at org.springframework.test.context.junit4.rules.SpringClassRule$TestContextManagerCacheEvictor.evaluate(SpringClassRule.java:230)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407) 2018-01-17 16:37:38,307 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@3cfdd820] for test class [class com.example.MySpring4RunnerTest] java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:103)
        at org.springframework.test.context.junit4.rules.SpringClassRule$TestContextManagerCacheEvictor.evaluate(SpringClassRule.java:230)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407) 2018-01-17 16:37:38,307 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@6f8e8894] for test class [class com.example.MySpring4RunnerTest] java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:103)
        at org.springframework.test.context.junit4.rules.SpringClassRule$TestContextManagerCacheEvictor.evaluate(SpringClassRule.java:230)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407) 2018-01-17 16:37:38,308 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@6b0d80ed] for test class [class com.example.MySpring4RunnerTest] java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.springframework.test.context.junit4.statements.ProfileValueChecker.evaluate(ProfileValueChecker.java:103)
        at org.springframework.test.context.junit4.rules.SpringClassRule$TestContextManagerCacheEvictor.evaluate(SpringClassRule.java:230)
        at org.junit.rules.RunRules.evaluate(RunRules.java:20)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.mockito.internal.runners.JUnit45AndHigherRunnerImpl.run(JUnit45AndHigherRunnerImpl.java:37)
        at org.mockito.runners.MockitoJUnitRunner.run(MockitoJUnitRunner.java:62)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407) [ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 1.381 s <<< FAILURE! - in com.example.MySpring4RunnerTest [ERROR] testBeanNames(com.example.MySpring4RunnerTest)  Time elapsed:
0.067 s  <<< ERROR! java.lang.ArrayIndexOutOfBoundsException: 1

[ERROR] com.example.MySpring4RunnerTest  Time elapsed: 0.146 s  <<< ERROR! java.lang.ArrayIndexOutOfBoundsException: 6

[INFO] [INFO] Results: [INFO] [ERROR] Errors: [ERROR]   MySpring4RunnerTest.com.example.MySpring4RunnerTest ▒ ArrayIndexOutOfBounds [ERROR]   MySpring4RunnerTest.testBeanNames ▒ ArrayIndexOutOfBounds 1 [INFO] [ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0 [INFO] [INFO]
------------------------------------------------------------------------ [INFO] BUILD FAILURE [INFO]
------------------------------------------------------------------------

As you can see none of the exception is associated with my code itself, but with the test context. Actually it seems to be associated with surefire plugin, as this test runs correctly from Eclipse but fails to run from the command line.

The environment that I am using also contains:

  • Maven v.3.3.9
  • jUnit:4.12 (I also tried with jUnit 5, vintage mode with no luck).
  • maven-surefire-plugin:2.20.1 (version 2.19.1 yields same results).
  • Eclipse is configured to use the external Maven 3.3.9 and not whatever the m2e plugin comes with.

Has anyone experienced similar behavior? is there a workaround this behavior?

Thanks in advance for the help

BobbaFett_666
  • 71
  • 1
  • 9
  • What's in `MyTestConfig`? – Jim Garrison Jan 18 '18 at 00:10
  • 1
    Have you tried running it with `SpringJUnit4ClassRunner` instead of `MockitoJUnitRunner`? – Arho Huttunen Jan 18 '18 at 03:02
  • MyTestConfig has almost nothing: `@Configuration @ComponentScan("com.cexample") public class MyTestConfig { public static final String TESTING_SPARK_MASTER = "local[*]"; public static final String TESTING_SPARK_APPNAME = "Spark Test Application"; @Bean public SparkSession sparkSession() { return SparkSession.builder() .master(TESTING_SPARK_MASTER) .appName(TESTING_SPARK_APPNAME) .getOrCreate(); } }` – BobbaFett_666 Jan 18 '18 at 16:12
  • Using `SpringJUnit4ClassRunner` or `SpringRunner` and commenting out `SpringClassRule + SpringMethodRule` has the same effect as it leads to the same TestContextManager portion where the ArrayIndexOutOfBoundsException happens. Additionally the use case that we have is to be able to use `@RunWith` with a runner other than `SpringRunner` or `SpringJUnit4ClassRunner` (let's say `MockitoJUnitRunner` or `Parameterized`) and still be able to bring the Spring Context into the text (which is achieved using the SpringClassRule + SpringMethodRule combo) – BobbaFett_666 Jan 18 '18 at 16:23
  • What version of the JDK are you using in the Maven build? – Sam Brannen Jan 19 '18 at 16:39
  • Can you please include a stack trace that actually shows where the `ArrayIndexOutOfBoundsException` is thrown that causes the failure? – Sam Brannen Jan 19 '18 at 16:39
  • Have you tried upgrading to a recent version of Mockito that explicitly supports Java 8? – Sam Brannen Jan 19 '18 at 16:46
  • 1.8 in pom.xml and 1.8.0_150 as the installed jdk – BobbaFett_666 Jan 19 '18 at 17:55
  • The stacktrace is posted in my original message, it happens when surefire.ForkedBooter.main tries to run my test, for some reason it reaches a point inside spring-test v5 TestContextManager that throws this exception. Further more, if I ONLY change spring-test version to 4.3.13.RELEASE and leave everything else as spring version 5.0.2.RELEASE it works. – BobbaFett_666 Jan 19 '18 at 17:55
  • It does not matter if I include mockito or not, I am not using it anyways. If I replace it for Parameterized.class or even if I use SpringJUnit4ClassRunner or SpringRunner it fails at the same spot. – BobbaFett_666 Jan 19 '18 at 17:55

2 Answers2

1

I was facing the same problem. It turned out it was caused by an old version of Jacoco configured in my project. It was not working properly with interfaces with default methods while instrumenting them. Once I upgraded the Jacoco to

<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifact>
<version>0.8.2-SNAPSHOT</version>

The problem has gone away.

Jose Da Silva Gomes
  • 3,814
  • 3
  • 24
  • 34
mfanta
  • 41
  • 3
0

UPDATE 01/19/2018:

I have minimized the test to:

@ContextConfiguration(classes = {MySpring4RunnerTest.MyConfig.class})
@RunWith(SpringJUnit4ClassRunner.class)
public class MySpring4RunnerTest {

  @Autowired
  private ApplicationContext appCtx;

  @Test
  public void testBeanNames() {
    String[] beans = this.appCtx.getBeanDefinitionNames();
    Arrays.sort(beans);
    System.out.println("\nSpring Managed Beans:");
    Stream.of(beans).forEach(it -> System.out.println(it));    
  }

  @Configuration
  static class MyConfig {
  }
}

No Mockito or Parameterized runner, just SpringJUnit4ClassRunner (SpringRunner makes no difference) and the full stacktrace that I get looks like this:

2018-01-19 11:38:06,561 ERROR [main] org.springframework.test.context.TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@17d919b6] to prepare test instance [com.example.MySpring4RunnerTest@6ca18a14]
java.lang.ArrayIndexOutOfBoundsException: 1
        at org.springframework.test.context.TestExecutionListener.prepareTestInstance(TestExecutionListener.java:96)
        at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.createTest(SpringJUnit4ClassRunner.java:227)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner$1.runReflectiveCall(SpringJUnit4ClassRunner.java:289)
        at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.methodBlock(SpringJUnit4ClassRunner.java:291)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:246)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:97)
        at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
        at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
        at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
        at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
        at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
        at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:70)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
2018-01-19 11:38:06,607 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener@222a59e6] for test class [class com.example.MySpring4RunnerTest]
java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
2018-01-19 11:38:06,608 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener@7d61eb55] for test class [class com.example.MySpring4RunnerTest]
java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
2018-01-19 11:38:06,612 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.support.DependencyInjectionTestExecutionListener@53f3bdbd] for test class [class com.example.MySpring4RunnerTest]
java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
2018-01-19 11:38:06,624 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@17d919b6] for test class [class com.example.MySpring4RunnerTest]
java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
2018-01-19 11:38:06,624 WARN  [main] org.springframework.test.context.TestContextManager - Caught exception while invoking 'afterTestClass' callback on TestExecutionListener [org.springframework.test.context.web.ServletTestExecutionListener@62656be4] for test class [class com.example.MySpring4RunnerTest]
java.lang.ArrayIndexOutOfBoundsException: 6
        at org.springframework.test.context.TestExecutionListener.afterTestClass(TestExecutionListener.java:201)
        at org.springframework.test.context.TestContextManager.afterTestClass(TestContextManager.java:481)
        at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:77)
        at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
        at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:190)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:369)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeWithRerun(JUnit4Provider.java:275)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:239)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:160)
        at org.apache.maven.surefire.booter.ForkedBooter.invokeProviderInSameClassLoader(ForkedBooter.java:373)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:334)
        at org.apache.maven.surefire.booter.ForkedBooter.execute(ForkedBooter.java:119)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:407)
[ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0, Time elapsed: 0.982 s <<< FAILURE! - in com.example.MySpring4RunnerTest
[ERROR] testBeanNames(com.example.MySpring4RunnerTest)  Time elapsed: 0.043 s  <<< ERROR!
java.lang.ArrayIndexOutOfBoundsException: 1

[ERROR] com.example.MySpring4RunnerTest  Time elapsed: 0.045 s  <<< ERROR!
java.lang.ArrayIndexOutOfBoundsException: 6

[INFO]
[INFO] Results:
[INFO]
[ERROR] Errors:
[ERROR]   MySpring4RunnerTest.com.example.MySpring4RunnerTest ▒ ArrayIndexOutOfBounds
[ERROR]   MySpring4RunnerTest.testBeanNames ▒ ArrayIndexOutOfBounds 1
[INFO]
[ERROR] Tests run: 2, Failures: 0, Errors: 2, Skipped: 0
[INFO]
[INFO] ------------------------------------------------------------------------
[INFO] BUILD FAILURE
[INFO] ------------------------------------------------------------------------

If the only change I make is spring-test version 5.0.2.RELEASE to 4.3.13.RELEASE, and keep everything else as 5.0.2.RELEASE it works. And it works from Eclipse no matter what version I use. I think it is because Eclipse does not run the test through surefire.

BobbaFett_666
  • 71
  • 1
  • 9
  • were you able to resolve the issue? I ran into an issue (https://stackoverflow.com/questions/49924515/noclassdeffounderror-tests-fail-during-maven-test-phase-when-surefire-forkcount) which appears to be similar to this one. My workaround was to set forkCount to 0, but that isn't ideal since I want the tests to run in parallel. – testphreak Apr 19 '18 at 15:29