[英]Spring Web Service Unit Tests: java.lang.IllegalStateExcepton: Failed to load Application Context
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. 当我尝试从Eclipse中运行我的单元测试时,我收到错误“java.lang.IllegalStateExcepton:无法加载应用程序上下文”。
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. 但无论我用@ContextConfiguration
做什么,我仍然会得到同样的错误。
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. applicationContext.xml文件位于文件夹/ etc / ws中,但即使我将副本放在同一个文件夹中,它仍然会给我错误。
I am very new to Java, Spring, Eclipse and Ant, and really don't know where this is going wrong. 我是Java,Spring,Eclipse和Ant的新手,真的不知道这出错了。
The problem is that you use an absolute path in your @ContextConfiguration
annotation. 问题是您在@ContextConfiguration
批注中使用绝对路径。 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). 您现在指定applicationContext.xml位于系统的根文件夹中(可能不是,因为您说它位于/ etc / ws)。 I see two possible solutions: 我看到两种可能的解决方案
@ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
使用@ContextConfiguration(locations={"/etc/ws/applicationContext.xml"})
@ContextConfiguration(locations={"classpath:applicationContext.xml"})
将applicationContext.xml文件移动到项目中(例如在WEB-INF文件夹中)并使用@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. 在第2种情况下,您必须确保将applicationContext.xml放在其中的目录位于类路径中。 In Eclipse this can be configured in your project properties. 在Eclipse中,可以在项目属性中配置它。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.