Testing with Spring and Maven: applicationContext

前端 未结 9 1546
-上瘾入骨i
-上瘾入骨i 2020-12-13 04:04

Seems that question old as world, but I still can\'t find out the solution..

I\'m trying to run simple test:

@RunWith(SpringJUnit4ClassRunner.class)
         


        
相关标签:
9条回答
  • 2020-12-13 04:37

    For some reason, I had the same issue and it worked when I ran maven test with the JDK6 instead of JDK8 (in my case this is a legacy application)

    If it helps anyone.

    0 讨论(0)
  • 2020-12-13 04:37

    I faced the same problem. For me the test was running successfully via eclipse. However, when I ran mvn test, it was giving me error: Failed to load ApplicationContext

    Fix: Added the src/test/resources directory to classpath of surefire plugin as-

    <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.10</version>
                <configuration>
                    <additionalClasspathElements>
                        <additionalClasspathElement>${project.basedir}/src/test/resources</additionalClasspathElement>
                    </additionalClasspathElements>      
                </configuration>
    

    Adding classpath to SureFire

    Hope it helps.

    0 讨论(0)
  • 2020-12-13 04:41

    I think best practice is to put your application context file for testing PersonsPopulateTest-context.xml under src/test/resources. This file will be copied into target/test-classes and you can refer to it in your test class as below:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"classpath:**/PersonsPopulateTest-context.xml"})
    @Transactional
    public class PersonsPopulateTest {
    
    }
    

    If you still want to refer to applicationContext.xml under src/main/resources, then you have to include it as follow:

    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = {"file:src/main/resources/applicationContext.xml"})
    @Transactional
    public class PersonsPopulateTest {
    
    }
    

    It worked for me.

    0 讨论(0)
  • 2020-12-13 04:44

    UPDATED: Actually in my case the problem was in Compile on Save option enabled. I use Netbeans, and the option was set to For test execution only. This value compiles changed files and replaces resources with new files. However, due to usage of application resources additionally to test resources, For test execution only produces incorrectly generated resources in target folder.

    Changing Compile on Save=For test execution only

    to Compile on Save=Disable fixes the problem.

    The text below is also correct, but sometimes does not work. It was working only till I restarted Netbeans IDE. However, the details of the reason of the problem are correct, hence, I prefer to leave the test.


    OLD: In my situation I have appContext-test.xml in src/main/resources. When I change any code and launch one unit test (not all of them) it recompiles and correctly executed. But if I launch same unit test again it is failed with the java.lang.IllegalStateException: Failed to load ApplicationContext.

    I have changed pom.xml from

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/test/java/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    </build>
    

    to

    <build>
        <resources>
            <resource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </resource>
        </resources>
        <testResources>
            <testResource>
                <directory>${project.basedir}/src/main/resources</directory>
                <filtering>true</filtering>
            </testResource>
            <testResource>
                <directory>${project.basedir}/src/test/java/resources</directory>
                <filtering>true</filtering>
            </testResource>
        </testResources>
    </build>
    

    and now all work fine.

    The error was due to appContext-test.xml uses src/main/resources/my.properties file with a lot of variables like

    database.url = ${database.url}
    database.username = ${database.username}
    database.password = ${database.password}
    

    that are filled during a build. However, if you skip src/main/resources in testResource, then my.properties is added to target/classes/my.properties as is, i.g. without a substitution. This file of course breaks the context.

    PS: You can remove ${project.basedir}/ - it is my custom thing.

    0 讨论(0)
  • 2020-12-13 04:49

    I had the same problem, all files were successfully copied to target/classes and target/test-classes, still spring could not find them.

    Problem disappeared when I explicitly specified versions for maven-surefire-plugin and maven-resources-plugin in pom

    0 讨论(0)
  • 2020-12-13 04:55
    <!-- Add this directly after the <build>-opening tag in the your pom-->
                <testResources>
                    <testResource>
                        <directory>src/test/resources</directory>
                        <filtering>true</filtering>
                        <includes>
                            <include>**/*.xml</include>
                            <include>**/*.properties</include>
                        </includes>
                    </testResource>
                </testResources>
    
    0 讨论(0)
提交回复
热议问题