【问题标题】:Java Project: Failed to load ApplicationContextJava 项目:无法加载 ApplicationContext
【发布时间】:2011-06-30 19:21:47
【问题描述】:

我有一个 Java 项目,我正在其中编写一个简单的 JUNIT 测试用例。我已将 appplicatinoContext.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);
        }
    }
}

【问题讨论】:

  • 错误是否明确表示找不到文件(FileNotFoundException)?你能粘贴堆栈跟踪吗?

标签: unit-testing spring junit


【解决方案1】:

看起来您正在使用 maven (src/main/java)。在这种情况下,将 applicationContext.xml 文件放在 src/main/resources 目录中。它将被复制到类路径目录中,您应该可以使用它访问它

@ContextConfiguration("/applicationContext.xml")

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

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

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

【讨论】:

  • 谢谢,您的建议奏效了。我将 applicationContext.xml 移动到资源目录,并将斜杠添加到上下文配置中。
  • 谢谢。有趣的是,Spring MVC 在 WEB-INF/ 文件夹中运行得很好,但 Junit 却摸不着头脑。
【解决方案2】:

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

<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>

测试在 IDE (eclipse sts) 中运行良好,但在使用命令 mvn test 时失败。

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

    <parallel>methods</parallel>
    <threadCount>10</threadCount>

希望这对某人有所帮助!

【讨论】:

    【解决方案3】:

    我遇到这个问题是因为during bootstrapping my spring project 使用实现ApplicationListener&lt;ContextRefreshedEvent&gt; 的类和onApplicationEvent 函数内部它会引发异常

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

    在我的例子中,我使用maven surefire plugin 进行测试,所以使用这个命令来调试测试过程

    mvn -Dmaven.surefire.debug test
    

    【讨论】:

      【解决方案4】:

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-10-07
        • 2013-04-30
        • 2021-11-29
        • 2019-06-20
        • 2021-05-24
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多