Java Project: Failed to load ApplicationContext

后端 未结 3 1636
情深已故
情深已故 2020-12-28 12:55

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 wit

相关标签:
3条回答
  • 2020-12-28 13:39

    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
    
    0 讨论(0)
  • 2020-12-28 13:48

    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!

    0 讨论(0)
  • 2020-12-28 13:51

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

    0 讨论(0)
提交回复
热议问题