Java 项目:无法加载 ApplicationContext

发布于 2024-10-18 10:02:32 字数 1107 浏览 2 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

心房敞 2024-10-25 10:02:32

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

@ContextConfiguration("/applicationContext.xml")

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 the applicationContext.xml file in the src/main/resources directory. It will be copied in the classpath directory and you should be able to access it with

@ContextConfiguration("/applicationContext.xml")

From 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).

铃予 2024-10-25 10:02:32

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

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

希望这可以帮助别人!

I had the same problem, and I was using the following plugin for tests:

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

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:

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

Hope that this helps someone out!

萌辣 2024-10-25 10:02:32

我遇到这个问题是因为在引导我的 spring 项目期间
使用实现 ApplicationListener 的类并在 onApplicationEvent 函数中抛出异常

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

在我的情况下不会引发任何异常,我使用的是 maven Surefire 插件 用于测试,因此要调试测试过程,请使用此命令

mvn -Dmaven.surefire.debug test

I've faced this issue because during bootstrapping my spring project
using the class that implements ApplicationListener<ContextRefreshedEvent> and inside onApplicationEvent function it throws an exception

so make sure that your application bootstrap points do not throw any exception

in my case, I was using maven surefire plugin for testing so to debug the test process use this command

mvn -Dmaven.surefire.debug test
长伴 2024-10-25 10:02:32

我遇到了这个问题,那就是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文