Testing with Spring and Maven: applicationContext

前端 未结 9 1547
-上瘾入骨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:57

    I think Maven simply didn't include the XML file from main/resources.

    You could try to specify explicitly what to include in the pom.xml.

    Let me know if the following configuration has worked:

        <!-- Add this directly after the <build>-opening tag -->
        <resources>
            <resource>
                <filtering>true</filtering>
                <directory>src/test/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                </includes>
                <excludes>
                    <exclude>**/*local.properties</exclude>
                </excludes>
            </resource>
            <resource>
                <directory>src/main/resources</directory>
                <includes>
                    <include>**/*.properties</include>
                    <include>**/*.xml</include>
                </includes>
            </resource>
        </resources>
    

    This is something I use in your case. You can edit this if you don't have properties files to be included.

    0 讨论(0)
  • 2020-12-13 05:01

    My test context file is under src\test\resources\spring folder. I managed to load the context with

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

    But reference (in test-context.xml) to the application-context.xml which is under src\main\resources\spring folder failed

    I managed to load the application-context by creating a ClassPathXmlApplicationContext in the test class with

    ClassPathXmlApplicationContext appContext=new ClassPathXmlApplicationContext(new String[]{"classpath:spring/application-context.xml","classpath:spring/model-context.xml"});
    

    Let me know if this helps or might create any other issues.

    0 讨论(0)
  • 2020-12-13 05:01

    your application context must be included in classpath and put * :

    @ContextConfiguration(locations = { "classpath:*/application-context.xml" })
    
    0 讨论(0)
提交回复
热议问题