2014-04-04 86 views
0

在Spring框架中運行腳本時,它將錯誤消息拋出爲「無法加載ApplicationContext」。我在src /測試/資源 我applicationContext.xml文件,我是用Spring框架:無法加載ApplicationContext

@RunWith(value = Parameterized.class) 
public class CustomerTest extends FunctionLibrary { ... } 

後從論壇上提出了一些建議,我用

@RunWith(value = Parameterized.class) 
@ContextConfiguration(locations = "file:src/test/resources/applicationContext.xml") 
public class CustomerTest extends FunctionLibrary { ... } 

我仍得到相同的錯誤消息。請任何人告訴我如何解決這個問題?

感謝,

Sudhansu

+3

您通常通過首先閱讀異常堆棧跟蹤來解決問題。 –

+2

對於初學者,您需要刪除@RunWith(Parameterized.class)並將其替換爲@RunWith(SpringJUnit4ClassRunner.class)(這意味着您的JUnit測試不會被參數化,並且您需要爲其找到其他方式參數化的功能) – geoand

+0

用@RunWith(Parameterized.class)替換@RunWith(SpringJUnit4ClassRunner.class)..它顯示類錯誤消息類不能解析類型..如何reslove這個錯誤信息? – user3411418

回答

0

有幾個問題

1)使用@RunWith(SpringJUnit4ClassRunner.class)

2)你的路徑聽起來像是你有一個Maven項目。在一個maven項目中,當測試運行時,src/test/resources將被添加到類路徑中。因此,使用:

@ContextConfiguration(locations = "classpath:applicationContext.xml") 

如果你的港灣,在src/main/resources的其他文件枝條同一個名字的話,我推薦一個在src/test/resources重命名爲test-applicationContext.xml,然後用@ContextConfiguration(locations = "classpath:test-applicationContext.xml"),只是爲了減少結的數量你腦

完整的示例:

package com.test.service; 

import org.junit.Assert; 
import org.junit.Test; 
import org.junit.runner.RunWith; 
import org.springframework.test.context.ContextConfiguration; 
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; 

@RunWith(SpringJUnit4ClassRunner.class) 
@ContextConfiguration(classpath:test-ApplicationContext.xml) 
public class WhateverTest { 
... 
} 

POM摘錄:

<dependency> 
    <groupId>junit</groupId> 
    <artifactId>junit</artifactId> 
    <version>4.11</version> 
    <scope>test</scope> 
</dependency> 
<dependency> 
     <groupId>org.springframework</groupId> 
     <artifactId>spring-test</artifactId> 
     <version>3.2.6.RELEASE</version> 
</dependency> 
+0

我已經添加了這兩個註解@RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations =「classpath:applicationContext.xml」),但它顯示Junit初始化錯誤...如何修復初始化錯誤..我認爲無法找到applicatioContext.xml文件的路徑 – user3411418

+0

@ user3411418:您需要發佈初始化錯誤 - 我無法猜測它們。 – Ralph

+0

java.lang.Exception:測試類應該只有一個公有的零參數構造函數 \t at org.junit.runners.BlockJUnit4ClassRunner.validateZeroArgConstructor(BlockJUnit4ClassRunner。Java的:147) \t在org.junit.runners.BlockJUnit4ClassRunner.validateConstructor(BlockJUnit4ClassRunner.java:124) \t在org.junit.runners.BlockJUnit4ClassRunner.collectInitializationErrors(BlockJUnit4ClassRunner.java:103) – user3411418

相關問題