解决Spring TestContext下运行JUnit4抛错(java.lang.NoClassDefFoundError)的问题

Spring版本2.5.5,JUnit 版本 4.8.1,使用了 Spring TestContext 的 SpringJUnit4ClassRunner。一直使用这个版本的JUnit,在写简单的测试时不会抛错,直到今天开始尝试写规范的JUnit测试代码,抛出了如下的错误:

java.lang.NoClassDefFoundError: org/junit/Assume$AssumptionViolatedException
      at org.springframework.test.context.junit4.SpringMethodRoadie.runTestMethod( SpringMethodRoadie.java:240)
      at org.springframework.test.context.junit4.SpringMethodRoadie$RunBeforesThenTestThenAfters.run( SpringMethodRoadie.java:333)
      at org.springframework.test.context.junit4.SpringMethodRoadie.runWithRepetitions( SpringMethodRoadie.java:217)
      at org.springframework.test.context.junit4.SpringMethodRoadie.runTest( SpringMethodRoadie.java:197)
      at org.springframework.test.context.junit4.SpringMethodRoadie.run( SpringMethodRoadie.java:143)
      at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.invokeTestMethod( SpringJUnit4ClassRunner.java:142)
      at org.junit.internal.runners.JUnit4ClassRunner.runMethods( JUnit4ClassRunner.java:61)
      at org.junit.internal.runners.JUnit4ClassRunner$1.run( JUnit4ClassRunner.java:54)
      at org.junit.internal.runners.ClassRoadie.runUnprotected( ClassRoadie.java:34)
      at org.junit.internal.runners.ClassRoadie.runProtected( ClassRoadie.java:44)
      at org.junit.internal.runners.JUnit4ClassRunner.run( JUnit4ClassRunner.java:52)
      at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run( JUnit4TestReference.java:50)
      at org.eclipse.jdt.internal.junit.runner.TestExecution.run( TestExecution.java:38)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:467)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests( RemoteTestRunner.java:683)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run( RemoteTestRunner.java:390)
      at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main( RemoteTestRunner.java:197)
Caused by: java.lang.ClassNotFoundException: org.junit.Assume$AssumptionViolatedException
      at java.net.URLClassLoader$1.run( URLClassLoader.java:202 )
      at java.security.AccessController.doPrivileged( Native Method )
      at java.net.URLClassLoader.findClass( URLClassLoader.java:190 )
      at java.lang.ClassLoader.loadClass( ClassLoader.java:306 )
      at sun.misc.Launcher$AppClassLoader.loadClass( Launcher.java:301 )
      at java.lang.ClassLoader.loadClass( ClassLoader.java:247 )
      ... 17 more

找了下网上的资料[1,2],这属于 Spring 与 JUnit 版本不兼容导致的。

解决方法:

修改JUnit 的版本至 4.4

---------------------------------------------------------------------------------------

[1] http://forum.springsource.org/showthread.php?60230-spring-test-breaks-in-Junit-4-5

[2] https://jira.springsource.org/browse/SPR-5145

你可能感兴趣的:(其他)