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

Xet*_*ius 3 java junit spring

当我尝试从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);
    }
}
Run Code Online (Sandbox Code Playgroud)

但不管我似乎做了什么,@ContextConfiguration我仍然得到同样的错误.

applicationContext.xml文件位于文件夹/ etc/ws中,但即使我将副本放在同一个文件夹中,它仍然会给我错误.

我是Java,Spring,Eclipse和Ant的新手,真的不知道这出错了.

Mar*_*urm 6

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

  1. 使用 @ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
  2. 将applicationContext.xml文件移动到项目中(例如在WEB-INF文件夹中)并使用 @ContextConfiguration(locations={"classpath:applicationContext.xml"})

在第2种情况下,您必须确保将applicationContext.xml放在其中的目录位于类路径中.在Eclipse中,可以在项目属性中配置它.