Spring Web服务单元测试:java.lang.IllegalStateExcepton:无法加载应用程序上下文

时间:2009-12-02 09:33:16

标签: java spring junit

当我尝试从Eclipse中运行单元测试时,我收到错误“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的新手,真的不知道这出错了。

1 个答案:

答案 0 :(得分:6)

问题是您在@ContextConfiguration注释中使用绝对路径。您现在指定applicationContext.xml位于系统的根文件夹中(可能不是,因为您说它位于/ etc / ws)。我看到两种可能的解决方案:

  1. 使用@ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
  2. 将您的applicationContext.xml文件移动到您的项目中(例如在您的WEB-INF文件夹中)并使用@ContextConfiguration(locations={"classpath:applicationContext.xml"})
  3. 在第2种情况下,您必须确保放置applicationContext.xml的目录位于类路径中。在Eclipse中,可以在项目属性中配置它。