Aritz
Aritz

Reputation: 31679

How to load Spring Application context in Maven tests

I have a Maven web project where I want to execute some JUnit integration tests. To achieve that, I want to load the applicationContext.xml file to obtain the Spring injected beans. Also my application context files are located into src/main/resources/app_context, but when executing Maven's lifecycle, it's locating them into WEB-INF/classes/application_context, so I'm able to access them through the classpath.

<resource>
    <directory>src/main/resources/appcontext</directory>
    <targetPath>${basedir}/src/main/webapp/WEB-INF/classes/application_context</targetPath>
    <filtering>true</filtering>
    <includes>
        <include>*/**</include>
    </includes>
</resource>

I have tried to access that context in some ways, but without succeeding. I'm also putting them into Maven's target test directory in that way:

<testResources>
    <testResource>
        <directory>
                src/main/resources/appcontext
            </directory>
        <targetPath>${basedir}/src/main/webapp/WEB-INF/classes/application_context</targetPath>
    </testResource>
</testResources>

This is a piece of my Test:

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

    @Autowired
    ApplicationContext applicationContext;

    @Test
    public void initializeSystemTest() {
            //Test code

And that's how I have access to the rest of the application context files from my main one:

<?xml version="1.0" encoding="ISO-8859-15"?>

<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:cxf="http://cxf.apache.org/core" xmlns:jaxws="http://cxf.apache.org/jaxws"
xsi:schemaLocation="
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.1.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.1.xsd">

    <import resource="classpath:application_context/ApplicationContext*.xml" />
    <context:annotation-config />
    <!--========== Spring Data Source ========== -->
    <!--Bean stuff-->

Basically I want to have only a copy of each one of the applicationContext files stored in src/main/resources and let Maven copy them for test and execution scopes. However my test classes are unable to find and load them. What I have to do?

Upvotes: 1

Views: 10694

Answers (1)

cowls
cowls

Reputation: 24354

Assuming your applicationContext.xml is in src/main/resources/appcontext, Maven will realocate it in WEB-INF/classes/appcontext by default. It's not necessary to assign a new path for that resource.

You should be able to find the context file in your test with:

@ContextConfiguration(locations={"/appcontext/applicationContext.xml"})

You shouldn't need additional Maven settings for this.

Upvotes: 2

Related Questions