2015-05-04 88 views
9

为什么我的弹簧测试设置失败,并在下面显示以下不太有用的错误消息?所有的建议表示赞赏。为什么使用SpringJUnit4ClassRunner.withAfterClasses方法的隐藏MultipleFailureException错误消息

JUnit的输出

java.lang.NoClassDefFoundError: org/junit/runners/model/MultipleFailureException 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.withAfterClasses(SpringJUnit4ClassRunner.java:188) 
at org.junit.runners.ParentRunner.classBlock(ParentRunner.java:145) 
at org.junit.runners.ParentRunner.run(ParentRunner.java:235) 
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run (SpringJUnit4ClassRunner.java:163) 
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:459) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:675) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:382) 
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:192) 
Caused by: java.lang.ClassNotFoundException: org.junit.runners.model.MultipleFailureException 
at java.net.URLClassLoader$1.run(Unknown Source) 
at java.net.URLClassLoader$1.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at java.net.URLClassLoader.findClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source) 
at java.lang.ClassLoader.loadClass(Unknown Source) 
... 10 more 

控制台输出

信息:org.springframework.test.context.support.DefaultTestContextBootstrapper - 默认加载TestExecutionListener的类名从位置[META-INF /spring.factories]:[org.springframework.test.context.web.ServletTestExecutionListener,org.springframework.test.context.support.DependencyInjectionTestExecutionListener,org.springf ramework.test.context.support.DirtiesContextTestExecutionListener,org.springframework.test.context.transaction.TransactionalTestExecutionListener,org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener] INFO:org.springframework.test.context.support.DefaultTestContextBootstrapper - 可以没有实例化TestExecutionListener [org.springframework.test.context.jdbc.SqlScriptsTestExecutionListener]。指定自定义侦听器类或使缺省侦听器类(及其必需的依赖项)可用。 Offending类:[org/springframework/transaction/interceptor/TransactionAttribute] INFO:org.springframework.test.context.support.DefaultTestContextBootstrapper - 无法实例化TestExecutionListener [org.springframework.test.context.transaction.TransactionalTestExecutionListener]。指定自定义侦听器类或使缺省侦听器类(及其必需的依赖项)可用。 Offending类:[org/springframework/transaction/interceptor/TransactionAttributeSource] INFO:org.springframework.test.context.support.DefaultTestContextBootstrapper - Using TestExecutionListeners:[or[email protected]76959acc,org.springframework .test.context.support.DependencyInjectionTestExecutionListener @ 57e603e6,org.springfra[email protected]3e0a1e1f]

这里是目标代码段

@Service 

public class PipApps { 

@Resource(name = "apps") 
private Properties apps; 

@Autowired 
private SitePreferenceHandler sitePreferenceHandler; 

@Autowired 
private PipsTable pipsTable; 

private SitePreference sitePreference; 

private Device device; 

public PipApps(HttpServletRequest request, HttpServletResponse response){ 
    sitePreference = sitePreferenceHandler.handleSitePreference(request, response); 
    device = DeviceUtils.getRequiredCurrentDevice(request); 
} 

public Properties getApps(){ 
    return apps; 
} 

public Device getDevice(){ 
    return device; 
} 

public SitePreference getSitePreference(){ 
    return sitePreference; 
} 

public DeviceRouteTable getPipsTable(){ 
    return pipsTable; 
} 
} 

和测试片断

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations={"src/test/resources/PipAppsTest-context.xml"}) 

public class PipAppsTest { 

@Mock 
SitePreferenceHandler sitePreferenceHandler; 

@Autowired 
PipApps pipApps; 

... 
} 
+0

什么也不是那么有用吗?从堆栈跟踪来看,您正在使用带有Spring Test的JUnit 4的非兼容版本。 –

+0

我正在使用JUnit 4.7。我现在已根据您的建议走了,并将其升级到最新的JUnit 4.12版。错误已停止发生。该错误信息虽然很神秘,因此也不是很有用。但我认为模糊背后有一个很好的理由。 – 000

回答

22

更新 - 2015年9月

Spring框架4.2.2抛出一个更有意义的异常,如果JUnit的4.9是不是在classpath中。详情请参阅SPR-13521


以下是类级别的javadoc获得SpringJUnit4ClassRunner的摘录:

注:由于Spring框架4.1,这个类,需要JUnit 4.9或更高版本。

有问题的类MultipleFailureException是在JUnit 4.9中引入的。

所以这就是为什么你的测试失败与ClassNotFoundException

升级到JUnit 4.9(或最好是4.12)可以解决您的问题。

问候,

山姆(Spring的TestContext框架的作者)

+0

嗨,我已经在我的项目中将Junit版本升级到4.9,但仍然出现同样的错误。 – Pruthviraj

+0

如果您从Spring获得需要JUnit 4.9的消息,那么您尚未成功升级项目以使用JUnit 4.9。例如,您可能会将不同版本的JUnit作为传递依赖项。因此,请验证JUnit如何被拉入您的依赖关系图中。 –