我有一个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);
}
}
}发布于 2011-02-21 02:02:31
看起来您使用的是maven (src/main/java)。在本例中,将applicationContext.xml文件放在src/main/resources目录中。它将被复制到类路径目录中,您应该能够通过以下命令访问它
@ContextConfiguration("/applicationContext.xml")从Spring-Documentation:一个普通路径,例如"context.xml",将被视为来自同一个包的类路径资源,测试类在这个包中定义为。以斜杠开头的路径被视为完全限定的类路径位置示例,例如“///config.xml”。
因此,在引用类路径根目录中的文件时,添加斜杠非常重要。
如果使用绝对文件路径,则必须使用' file :C:...‘(如果我正确理解文档的话)。
发布于 2011-10-03 21:06:33
我也遇到了同样的问题,我使用以下插件进行测试:
<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>测试在集成开发环境(eclipse sts)中运行良好,但在使用命令mvn test时失败。
经过大量的试验和错误,我认为解决方案是删除并行测试,从上面的插件配置中删除以下两行:
<parallel>methods</parallel>
<threadCount>10</threadCount>希望这对某些人有所帮助!
发布于 2017-10-18 03:21:53
我遇到过这个问题,因为during bootstrapping my spring project使用实现ApplicationListener<ContextRefreshedEvent>的类,在onApplicationEvent函数中抛出异常
,因此请确保应用程序引导点不会抛出任何异常
在我的示例中,我使用maven surefire plugin进行测试,因此要调试测试过程,请使用以下命令
mvn -Dmaven.surefire.debug testhttps://stackoverflow.com/questions/5058294
复制相似问题