Java 项目:无法加载 ApplicationContext
我有一个 Java 项目,我正在其中编写一个简单的 JUNIT 测试用例。我已将 applicatinoContext.xml 文件复制到根 java 源目录中。我已经尝试了一些我在 StackOverflow 上读到的推荐设置,但仍然遇到相同的错误。发生此错误是因为我的项目是 java 项目而不是 Web 项目,还是这很重要?我不确定我哪里出错了。
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"C:/projs/sortation/src/main/java/applicationContext.xml"})
// Also tried these settings but they also didnt work,
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})
//@ContextConfiguration("classpath:applicationContext.xml")
@Transactional
public class TestSS {
@Autowired
private EmsDao dao;
@Test
public void getSites() {
List<String> batchid = dao.getList();
for (String s : batchid) {
System.out.println(s);
}
}
}
I have a Java Project in which I am writing a simple JUNIT test case. I have copied the applicatinoContext.xml file into the root java source directory. I've tried it with some of the recommended settings I have read of here on StackOverflow but still get the same error. Is this error happening due to my project being a java project and NOT a web project, or does that even matter? I'm not sure where im going wrong.
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations={"C:/projs/sortation/src/main/java/applicationContext.xml"})
// Also tried these settings but they also didnt work,
//@ContextConfiguration(locations={"classpath:applicationContext.xml"})
//@ContextConfiguration("classpath:applicationContext.xml")
@Transactional
public class TestSS {
@Autowired
private EmsDao dao;
@Test
public void getSites() {
List<String> batchid = dao.getList();
for (String s : batchid) {
System.out.println(s);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
看起来您正在使用 maven (
src/main/java)
。在本例中,将applicationContext.xml
文件放在src/main/resources
目录中。它将被复制到类路径目录中,您应该能够使用From the Spring-Documentation:普通路径,例如“context.xml”,将被视为类路径资源来自定义测试类的同一包。以斜杠开头的路径被视为完全限定的类路径位置,例如“/org/example/config.xml”。
因此,在引用类路径根目录中的文件时添加斜杠非常重要。
如果您使用绝对文件路径,则必须使用“file:C:...”(如果我正确理解文档)。
Looks like you are using maven (
src/main/java)
. In this case put theapplicationContext.xml
file in thesrc/main/resources
directory. It will be copied in the classpath directory and you should be able to access it withFrom the Spring-Documentation: A plain path, for example "context.xml", will be treated as a classpath resource from the same package in which the test class is defined. A path starting with a slash is treated as a fully qualified classpath location, for example "/org/example/config.xml".
So it's important that you add the slash when referencing the file in the root directory of the classpath.
If you work with the absolute file path you have to use 'file:C:...' (if I understand the documentation correctly).
我遇到了同样的问题,我使用以下插件进行测试:
测试在 IDE (eclipse sts) 中运行良好,但在使用命令 mvn test 时失败。
经过大量的试验和错误,我认为解决方案是删除并行测试,上面的插件配置中的以下两行:
希望这可以帮助别人!
I had the same problem, and I was using the following plugin for tests:
The test were running fine in the IDE (eclipse sts), but failed when using command mvn test.
After a lot of trial and error, I figured the solution was to remove parallel testing, the following two lines from the plugin configuration above:
Hope that this helps someone out!
我遇到这个问题是因为
在引导我的 spring 项目期间
使用实现
ApplicationListener
的类并在onApplicationEvent
函数中抛出异常在我的情况下不会引发任何异常,我使用的是 maven Surefire 插件 用于测试,因此要调试测试过程,请使用此命令
I've faced this issue because
during bootstrapping my spring project
using the class that implements
ApplicationListener<ContextRefreshedEvent>
and insideonApplicationEvent
function it throws an exceptionin my case, I was using maven surefire plugin for testing so to debug the test process use this command
我遇到了这个问题,那就是 Bean (@Bean) 没有正确实例化,因为我的测试类中没有给它正确的参数。
I faced this issue, and that is when a Bean (@Bean) was not instantiated properly as it was not given the correct parameters in my test class.