首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Java项目:未能加载ApplicationContext

Java项目:未能加载ApplicationContext
EN

Stack Overflow用户
提问于 2011-02-21 00:24:05
回答 4查看 201.9K关注 0票数 37

我有一个Java项目,其中我正在编写一个简单的JUNIT测试用例。我已经将applicatinoContext.xml文件复制到java源目录的根目录中。我已经尝试了一些我在StackOverflow上读到的推荐设置,但仍然得到相同的错误。发生这个错误是因为我的项目是java项目而不是web项目吗,或者这有什么关系吗?我不知道我错在哪里。

代码语言:javascript
复制
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);
        }
    }
}
EN

回答 4

Stack Overflow用户

回答已采纳

发布于 2011-02-21 02:02:31

看起来您使用的是maven (src/main/java)。在本例中,将applicationContext.xml文件放在src/main/resources目录中。它将被复制到类路径目录中,您应该能够通过以下命令访问它

代码语言:javascript
复制
@ContextConfiguration("/applicationContext.xml")

Spring-Documentation:一个普通路径,例如"context.xml",将被视为来自同一个包的类路径资源,测试类在这个包中定义为。以斜杠开头的路径被视为完全限定的类路径位置示例,例如“///config.xml”。

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

如果使用绝对文件路径,则必须使用' file :C:...‘(如果我正确理解文档的话)。

票数 28
EN

Stack Overflow用户

发布于 2011-10-03 21:06:33

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

代码语言:javascript
复制
<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时失败。

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

代码语言:javascript
复制
    <parallel>methods</parallel>
    <threadCount>10</threadCount>

希望这对某些人有所帮助!

票数 3
EN

Stack Overflow用户

发布于 2017-10-18 03:21:53

我遇到过这个问题,因为during bootstrapping my spring project使用实现ApplicationListener<ContextRefreshedEvent>的类,在onApplicationEvent函数中抛出异常

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

在我的示例中,我使用maven surefire plugin进行测试,因此要调试测试过程,请使用以下命令

代码语言:javascript
复制
mvn -Dmaven.surefire.debug test
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5058294

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档