2009-12-02 61 views
3

當我嘗試從Eclipse中運行單元測試時,出現錯誤「java.lang.IllegalStateExcepton:無法加載應用程序上下文」 。Spring Web服務單元測試:java.lang.IllegalStateExcepton:無法加載應用程序上下文

單元測試本身似乎很簡單:

package com.mycompany.interactive.cs.isales.ispr.ws.productupgrade; 

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(locations={"/applicationContext.xml"}) 
public class ProductUpgradeTest { 

    @Test 
    public void getProductUpgrade() throws Exception 
    { 
     //System.out.println(PrintClasspath.getClasspathAsString()); 
     Assert.assertTrue(true); 
    } 
} 

但無論怎樣,我似乎與@ContextConfiguration做我仍然得到同樣的錯誤。

applicationContext.xml文件駐留在一個文件夾/ etc/ws中,但即使我將一個副本放在同一個文件夾中,仍然會給我帶來錯誤。

我對Java,Spring,Eclipse和Ant非常陌生,真的不知道哪裏出了問題。

回答

6

問題是你在你的@ContextConfiguration註釋中使用絕對路徑。你現在指定你的applicationContext.xml位於你的系統的根文件夾(它可能不是,因爲你說它在/ etc/ws中)。我看到了兩個可能的解決方案:

  1. 使用@ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
  2. 移動你的applicationContext.xml文件到您的項目(例如,在你的WEB-INF文件夾中),並使用@ContextConfiguration(locations={"classpath:applicationContext.xml"})

在第二種情況下,你必須確保放置applicationContext.xml的目錄位於您的類路徑中。在Eclipse中,可以在您的項目屬性中進行配置。

相關問題