2009-12-02 60 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中,可以在您的项目属性中进行配置。

相关问题