SpringJUnit4ClassRunner在Spring5中被打破了吗?

时间:2018-01-17 23:57:28

标签: java spring junit spring-test maven-surefire-plugin

使用Spring 4.3.13和5.0.2版本运行测试时,我遇到了一种奇怪的行为。

鉴于以下简单测试,我只对确保可以将上下文加载到测试中感兴趣:

@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));    
  }
}

这与弹簧测试一样正常:4.3.13.RELEASE:

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

但是,如果我将Spring版本更改为5.0.2.RELEASE(是的,包括spring-test),我会收到以下异常消息:

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]
------------------------------------------------------------------------

正如您所看到的,没有任何异常与我的代码本身相关联,而是与测试上下文相关联。 实际上它似乎与surefire插件相关联,因为此测试从Eclipse正确运行但无法从命令行运行。

我正在使用的环境还包含:

  • Maven v.3.3.9
  • jUnit:4.12(我也试过jUnit 5,老式模式没有运气)。
  • maven-surefire-plugin:2.20.1(版本2.19.1产生相同的结果)。
  • Eclipse配置为使用外部Maven 3.3.9而不是m2e插件附带的任何内容。

有没有人经历过类似的行为?这种行为有解决办法吗?

提前感谢您的帮助

2 个答案:

答案 0 :(得分:0)

更新01/19/2018:

我已将测试最小化为:

@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 {
  }
}

没有Mockito或参数化的跑步者,只是SpringJUnit4ClassRunner(SpringRunner没有区别),我得到的完整堆栈跟踪看起来像这样:

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] ------------------------------------------------------------------------

如果我做的唯一更改是spring-test版本5.0.2.RELEASE到4.3.13.RELEASE,并保留其他所有内容为5.0.2.RELEASE它的工作原理。 无论我使用什么版本,它都可以在Eclipse中运行。我认为这是因为Eclipse没有通过surefire进行测试。

答案 1 :(得分:0)

我遇到了同样的问题。事实证明这是由我的项目中配置的旧版Jacoco引起的。在检测它们时,它与使用默认方法的接口无法正常工作。一旦我将Jacoco升级为

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

问题已经消失。