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..
If your test relies on system properties you could set them and unset them in 'before' and 'after' lifecycle methods. In Junit5, setting system properties for all tests in a test case might look like this: @BeforeAll public static void setSystemProperties() { // set the system properties // ... }
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.
In Eclipse, right-click the JUnit test class, select Run As > Run Configurations..., then go to Arguments tab, and under VM Arguments, add in the system property entry, e.g. -Dcatalina.base=C:\programs\apache-tomcat-7.0.32
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With