Set System Property for JUnit Runner (Eclipse) to test a Spring Web App

Pete picture Pete · Jun 12, 2012 · Viewed 50.9k times · Source

Our web app uses SystemPropertyPlaceholder to load property files depending on the value of a system property (see below)

The default setup for running it locally is stored in application.properties. On the production server we currently just set "env" to "production" before deploying the app and it will load production.properties.

Now for testing the app a test.properties file should be used.

If I run all of the tests say in our jenkins build, adding -Denv=test will work as expected. But what if I just want to run a single test in Eclipse with the integrated JUnit runner?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = WebContextLoader.class, locations = {"classpath:application-context.xml" })
public class SomeTest {

Is there any way to tell my test it should set the system property "env" to "test" BEFORE Spring is loaded up? Because using MethodInvokingFactoryBean will only set it afterwards for some reason, even if I set it before loading my property files:

<bean id="systemPrereqs"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" value="#{@systemProperties}" />
    <property name="targetMethod" value="putAll" />
    <property name="arguments">
        <!-- The new Properties -->
        <util:properties>
            <prop key="env">test</prop>
        </util:properties>
    </property>
</bean>

<bean
    class="org.springframework.web.context.support.ServletContextPropertyPlaceholderConfigurer">
    <property name="systemPropertiesModeName" value="SYSTEM_PROPERTIES_MODE_OVERRIDE" />
    <property name="searchContextAttributes" value="true" />
    <property name="contextOverride" value="true" />
    <property name="ignoreResourceNotFound" value="true" />
    <property name="locations">
        <list>
            <value>classpath:application.properties</value>
            <value>classpath:${env}.properties</value>
            <value>${config}</value>
        </list>
    </property>
</bean>

<bean id="managerDataSource"
    class="org.springframework.jdbc.datasource.DriverManagerDataSource">
    <property name="driverClassName" value="com.mysql.jdbc.Driver" />
    <property name="username">
        <value>${database.username}</value>
    </property>
    <property name="password">
        <value>${database.password}</value>
    </property>
    <property name="url">
        <value>${database.url}</value>
    </property>

</bean>

With the database properties defined inside application.properties, production.properties and test.properties.

The point is, of course that I want to use the same context file for all environments, otherwise I could just tell my test to use a different context where I set The PropertyPlaceholder property "location" to test.properties... But I want my tests to also cover my context so that any errors there are caught as early as possible (I'm doing end to end tests on our web app with spring-web-mvc which loads up the entire web app providing some nice feedback there and I don't want to loose that).

So far the only way I can see might be to configure the JUnit runner to include some system property setting argument, though I don't know how to do that..

Answer

AlexR picture AlexR · Jun 12, 2012

I am working on exactly the same problem now and hopefully found the way. You can call System.setProperty() into the static initializer of your test case.