Spring Web 服务单元测试:java.lang.IllegalStateExcepton:无法加载应用程序上下文
当我尝试从 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 都很陌生,真的不知道哪里出了问题。
I am getting the error "java.lang.IllegalStateExcepton: Failed to load Application Context" when I am trying to run my unit test from within Eclipse.
The Unit Test itself seems very simple:
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);
}
}
But no matter what I seem to do with the @ContextConfiguration
I still get the same error.
The applicationContext.xml file resides within a folder /etc/ws, but even if I put a copy in the same folder it is still giving me errors.
I am very new to Java, Spring, Eclipse and Ant, and really don't know where this is going wrong.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
问题是您在
@ContextConfiguration
注释中使用绝对路径。现在,您指定 applicationContext.xml 位于系统的根文件夹中(它可能不是,因为您说它位于 /etc/ws)。我看到两种可能的解决方案:@ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
在情况 2 中,您必须确保放置 applicationContext.xml 的目录位于您的类路径中。在 Eclipse 中,可以在项目属性中进行配置。
The problem is that you use an absolute path in your
@ContextConfiguration
annotation. You now specify that your applicationContext.xml is located in your root folder of your system (where it probably isn't, since you say it is at /etc/ws). I see two possible solutions:@ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
In case 2, you have to make sure that the directory in which you place your applicationContext.xml is located in your classpath. In Eclipse this can be configured in your project properties.