简体   繁体   English

无法从测试中加载Spring应用程序上下文

[英]can not load Spring application context from tests

I have the next project structure: 我有下一个项目结构:

src/
   main/
       java/
           DAO/Dao classes
           data/data classes
       resources/
           application-context.xml
    test/
        java/
            data/ Test files
        resources/
               application-context-test.xml

When I load context outside tests i use: 当我在测试之外加载上下文时,我使用:

ApplicationContext context = new ClassPathXmlApplicationContext("application-context.xml");

and it works.And if use 它可以工作。如果使用

@ContextConfiguration(locations = {"classpath:application-context.xml"}) 

inside tests - it works. 内部测试-它有效。 But if I use for: 但是如果我用于:

@ContextConfiguration(locations = {"classpath:application-context-test.xml"}) 

I got exception: 我有例外:

java.lang.IllegalStateException: Failed to load ApplicationContext

Any ideas why? 有什么想法吗? Maybe something is wrong with XML file? 也许XML文件有问题?

Make sure both your main and test directories are set up as source directories in eclipse. 确保将主目录和测试目录都设置为eclipse中的源目录。 You can do this from the ui, but you can also open the hidden .classpath file at the root of your application source. 您可以从ui中执行此操作,但也可以在应用程序源的根目录下打开隐藏的.classpath文件。 The beginning should look something like what you see before where the paths match your file structure: 开头应该看起来像您在路径匹配文件结构之前看到的内容:

<?xml version="1.0" encoding="UTF-8"?>
<classpath>
    <classpathentry kind="src" path="src/main/java"/>
    <classpathentry kind="src" path="src/test/java"/>
    <classpathentry kind="src" path="src/main/resources"/>
    <classpathentry kind="src" path="src/test/resources"/>
    .
    .
    .
</classpath>

If it does, the tests should work. 如果是这样,则测试应该可以进行。 If not they won't. 如果没有,他们不会。

You can do this from the UI by right clicking on the src/main/resources directory and selecting Build Path >> Add to Build Path. 您可以通过右键单击src / main / resources目录并选择Build Path >> Add to Build Path,从UI中执行此操作。 Your package explorer should look something like this with each of the source directories at the top. 您的程序包浏览器应该看起来像这样,每个源目录都在顶部。 在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从字符串加载spring应用程序上下文 - load spring application context from string Spring Application Context Load Order - Spring Application Context Load Order 如何加载Spring Application Context - How to load Spring Application Context 无法在Spring的应用程序上下文中加载多个属性文件 - Can't load multiple property files in spring's application context Spring Application Context无法加载配置文件 - Spring Application Context can't load the configuration files Spring Web服务单元测试:java.lang.IllegalStateExcepton:无法加载应用程序上下文 - Spring Web Service Unit Tests: java.lang.IllegalStateExcepton: Failed to load Application Context 如何在运行时使用 Cucumber 而不是通过测试加载 Spring Boot 应用程序上下文? - How to load Spring Boot Application Context while using Cucumber at runtime, not via tests? 如何在Spring 2.5中从字符串加载应用程序上下文? - How to load application context from string in Spring 2.5? 从xml应用程序上下文中弹簧加载命令行参数 - Spring load command line arguments from xml application context 如何从WEB-INF加载“迷你”弹簧上下文以初始化真实的应用程序上下文 - how to load a 'mini' spring context from WEB-INF in order to initialize the real application context
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM