Unit testing Java项目:未能加载ApplicationContext

Unit testing Java项目:未能加载ApplicationContext,unit-testing,spring,junit,Unit Testing,Spring,Junit,我有一个Java项目,我正在其中编写一个简单的JUNIT测试用例。我已将applicationContext.xml文件复制到根java源目录中。我在StackOverflow上读到了一些推荐设置,我已经尝试过了,但仍然得到了相同的错误。发生这个错误是因为我的项目是java项目而不是web项目,还是这很重要?我不知道我哪里出错了 import org.junit.Test; import org.junit.runner.RunWith; import org.springframework.b

我有一个Java项目,我正在其中编写一个简单的JUNIT测试用例。我已将applicationContext.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);
        }
    }
}
import org.junit.Test;
导入org.junit.runner.RunWith;
导入org.springframework.beans.factory.annotation.Autowired;
导入org.springframework.test.context.ContextConfiguration;
导入org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
导入org.springframework.transaction.annotation.Transactional;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(位置={“C:/projs/sortation/src/main/java/applicationContext.xml})
//也尝试过这些设置,但也不起作用,
//@ContextConfiguration(位置={“classpath:applicationContext.xml”})
//@ContextConfiguration(“类路径:applicationContext.xml”)
@交易的
公共类测试{
@自动连线
私人恩斯道道;
@试验
公共网站(){
List batchid=dao.getList();
for(字符串s:batchid){
系统输出打印项次;
}
}
}

看起来您正在使用maven(
src/main/java)
。在本例中,将
applicationContext.xml
文件放在
src/main/resources
目录中。它将被复制到classpath目录中,您应该能够使用

@ContextConfiguration("/applicationContext.xml")
从:普通路径,例如“context.xml”,将被视为来自定义测试类的同一包的类路径资源。以斜杠开头的路径被视为完全限定的类路径位置,例如“/org/example/config.xml”

因此,在类路径的根目录中引用文件时,添加斜杠非常重要


如果使用绝对文件路径,则必须使用“file:C:…”(如果我正确理解文档)。

我也遇到了同样的问题,我使用以下插件进行测试:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.9</version>
    <configuration>
        <useFile>true</useFile>
        <includes>
            <include>**/*Tests.java</include>
            <include>**/*Test.java</include>
        </includes>
        <excludes>
            <exclude>**/Abstract*.java</exclude>
        </excludes>
        <junitArtifactName>junit:junit</junitArtifactName>
        <parallel>methods</parallel>
        <threadCount>10</threadCount>
    </configuration>
</plugin>

org.apache.maven.plugins
maven surefire插件
2.9
真的
**/*Tests.java
**/*Test.java
**/抽象*.java
junit:junit
方法
10
测试在IDE(eclipse sts)中运行正常,但在使用命令mvn test时失败

经过多次尝试和错误,我认为解决方案是删除并行测试,从上面的插件配置中删除以下两行:

    <parallel>methods</parallel>
    <threadCount>10</threadCount>
方法
10

希望这能帮到别人

我面临这个问题是因为
在启动我的spring项目时
使用实现了
ApplicationListener
的类,并在applicationEvent函数的
内部抛出异常

因此,请确保应用程序引导点不会引发任何异常

在我的例子中,我使用了for testing,所以要调试测试过程,请使用以下命令

mvn -Dmaven.surefire.debug test

我遇到了这个问题,那就是Bean(@Bean)没有正确实例化,因为在我的测试类中没有为它提供正确的参数。

错误是否明确表示它找不到文件(
FileNotFoundException
)?你能粘贴堆栈跟踪吗?谢谢,你的建议奏效了。我将applicationContext.xml移到了resources目录,并在上下文配置中添加了斜杠。谢谢。有趣的是,SpringMVC在WEB-INF/文件夹中运行得很好,但Junit却让它头痛不已。