Roy Chan
Roy Chan

Reputation: 2918

Spring Unit/Intergration Testing setup

I didn't write unit or integration testing but now I am trying. I am having a hard time setting up the environment.

I have my application context under WEB-INF/applicationContext*.xml and in my applicationContext.xml, it has a reference to a properties file for DB user/pass, LDAP host, etc

<bean id="propertyConfigurer"
            class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
        <property name="locations">
            <list>
                <value>/WEB-INF/spring-config/dev.properties</value>
            </list>
        </property>
    </bean>

I have another properties for log4j config (diff config for DEV/Staging/Production). ${webapp.root} is defined in web.xml

 <!-- log4j setting -->
    <bean id="log4jInitialization" class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
        <property name="targetClass" value="org.springframework.util.Log4jConfigurer" />
        <property name="targetMethod" value="initLogging" />
        <property name="arguments">
            <list>
                <value>${webapp.root}/${log4j.properties.location}</value>
            </list>
        </property>
    </bean>

And now I am trying to put the following in a test class.

@Override
protected String[] getConfigLocations() {
   return new String[]{
            "file:trunk/code/web/WEB-INF/applicationContext.xml",
        };
}

This references my xml correctly, but all the properties are screwed up.

I want to know the following:

Please Advise

Thanks

Upvotes: 1

Views: 4191

Answers (3)

ceiroa
ceiroa

Reputation: 5973

You could use annotations to reference the necessary configuration from the tests, like this:

@RunWith(SpringJUnit4ClassRunner.class)
@TestExecutionListeners({
     DependencyInjectionTestExecutionListener.class,
     DirtiesContextTestExecutionListener.class,
     TransactionalTestExecutionListener.class })
@ContextConfiguration(locations = {
     "file:../WebService/src/main/resources/application-context.xml",
     "file:../ServiceLayer/src/test/resources/ServiceLayer-dao-test-context.xml" })
public class MyTest {
     // class body...
}

Upvotes: 1

Bozho
Bozho

Reputation: 597244

This blogpost of mine describes the basic steps to achieve your goal.

Note that the unit tests shouldn't know that you have a webapp-root - they are usually run without any servlet container started. So place the alternative config files in the test packages and try.

Upvotes: 6

Mark
Mark

Reputation: 29139

For unit testing you should not be using the Spring application context. You should be testing all your spring beans and controllers individually as they are the individual units within the system. As they are POJOs it is easy to wire everything together programatically in your test case code. The also solves issues such as the location of the logging properties file as you can programatically specify a different path that does not rely on the webroot property.

The testing chapter in the Spring Reference provides a good overview of how to approach unit and integration testing of applications that use Spring. It also provides details of the various support classes that Spring provides to help with writing unit and integration tests.

Upvotes: 1

Related Questions