2015-05-04 55 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如何被拉入您的依賴關係圖中。 –