Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/342.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringWeb服务单元测试:java.lang.IllegalStateExcepton:未能加载应用程序上下文_Java_Spring_Junit - Fatal编程技术网

SpringWeb服务单元测试:java.lang.IllegalStateExcepton:未能加载应用程序上下文

SpringWeb服务单元测试:java.lang.IllegalStateExcepton:未能加载应用程序上下文,java,spring,junit,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.spri

当我试图从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非常陌生,真的不知道哪里出了问题。

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

  • 使用
    @ContextConfiguration(locations={”/etc/ws/applicationContext.xml})
  • 将applicationContext.xml文件移动到项目中(例如在WEB-INF文件夹中),并使用
    @ContextConfiguration(locations={“classpath:applicationContext.xml”})
  • 在案例2中,必须确保放置applicationContext.xml的目录位于类路径中。在Eclipse中,这可以在项目属性中配置