100

I'd like to write some tests that check the XML Spring configuration of a deployed WAR. Unfortunately some beans require that some environment variables or system properties are set. How can I set an environment variable before the spring beans are initialized when using the convenient test style with @ContextConfiguration?

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext { ... }

If I configure the application context with annotations, I don't see a hook where I can do something before the spring context is initialized.

Hans-Peter Störr
  • 22,852
  • 27
  • 96
  • 134

7 Answers7

134

You can initialize the System property in a static initializer:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
public class TestWarSpringContext {

    static {
        System.setProperty("myproperty", "foo");
    }

}

The static initializer code will be executed before the spring application context is initialized.

Jimmy Praet
  • 1,890
  • 1
  • 12
  • 14
  • 19
    Silly me - OK, that would work. Even better: probably a `@BeforeClass` method to set the system property and an `@AfterClass` method to remove it would also work, and nicely clean up after itself. (Didn't try it out, though.) – Hans-Peter Störr Aug 27 '12 at 15:39
  • 1
    Tried the @BeforeClass - and it worked fine for setting system properties before other properties were set in the test instance – wbdarby Apr 02 '14 at 14:08
  • Thanks for this. The static thing didnt work but a small method with @BeforeClass worked ! – Midhun Agnihotram Dec 30 '16 at 04:31
  • This mechanism does not work if changing Log4j2 configuration file property. Seems that Spring anyway is being loaded (and so logging incorrectly) before that static piece of code. – lucasvc Mar 13 '19 at 11:35
93

The right way to do this, starting with Spring 4.1, is to use a @TestPropertySource annotation.

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath:whereever/context.xml")
@TestPropertySource(properties = {"myproperty = foo"})
public class TestWarSpringContext {
    ...    
}

See @TestPropertySource in the Spring docs and Javadocs.

Raman
  • 13,024
  • 3
  • 72
  • 95
  • 3
    This annotation also supports a properties file path. – MigDus May 05 '16 at 14:44
  • 2
    I could switch the Spring Cloud Config Client label during tests using `@TestPropertySource(properties={"spring.cloud.config.label=feature/branch"})` – Marcello de Sales Sep 19 '16 at 02:32
  • 6
    Good answer, but sadly didn't work for me, using Spring 4.2.9, the property was always empty. Only the static block worked... Worked for application properties, but not for system properties. – Gregor Feb 06 '18 at 14:56
  • First I saw and tried the static version (which worked), but this Annotation is even cleaner und much more preferable (for me, as it also works like a charm). – BAERUS May 08 '18 at 07:17
  • 5
    This provides an `Environment` property, which is different to an "Environment variable". – OrangeDog Aug 02 '19 at 13:15
  • will this remove the property (myproperty) after the test is executed? – Sajith Oct 03 '19 at 05:25
14

One can also use a test ApplicationContextInitializer to initialize a system property:

public class TestApplicationContextInitializer implements ApplicationContextInitializer<ConfigurableApplicationContext>
{
    @Override
    public void initialize(ConfigurableApplicationContext applicationContext)
    {
        System.setProperty("myproperty", "value");
    }
}

and then configure it on the test class in addition to the Spring context config file locations:

@ContextConfiguration(initializers = TestApplicationContextInitializer.class, locations = "classpath:whereever/context.xml", ...)
@RunWith(SpringJUnit4ClassRunner.class)
public class SomeTest
{
...
}

This way code duplication can be avoided if a certain system property should be set for all the unit tests.

anre
  • 3,348
  • 24
  • 32
  • This also works perfectly with Spring Boot 2.x and Junit 5.x (using `@SpringBootTest` or any of the [test slicing](https://spring.io/blog/2016/08/30/custom-test-slice-with-spring-boot-1-4) annotations) – Wim Deblauwe Jan 10 '20 at 16:54
12

All of the answers here currently only talk about the system properties which are different from the environment variables that are more complex to set, esp. for tests. Thankfully, below class can be used for that and the class docs has good examples

EnvironmentVariables.html

A quick example from the docs, modified to work with @SpringBootTest

@SpringBootTest
public class EnvironmentVariablesTest {
   @ClassRule
   public final EnvironmentVariables environmentVariables = new EnvironmentVariables().set("name", "value");

   @Test
   public void test() {
     assertEquals("value", System.getenv("name"));
   }
 }
Harish Gokavarapu
  • 1,347
  • 15
  • 11
  • The EnvironmentVariables rules is part of a third party library, uses hacky reflection to change the cached values of the environment in JVM memory and does not even the the actual environment variables. So, I would not like to use it or recommend anyone to do so. – Christian Jun 29 '20 at 10:40
  • It also seems to have a ProvideSystemProperty rule and, weirdly, a RestoreSystemProperties rule. So that could work for system properties, too. – Hans-Peter Störr May 07 '21 at 06:33
6

If you want your variables to be valid for all tests, you can have an application.properties file in your test resources directory (by default: src/test/resources) which will look something like this:

MYPROPERTY=foo

This will then be loaded and used unless you have definitions via @TestPropertySource or a similar method - the exact order in which properties are loaded can be found in the Spring documentation chapter 24. Externalized Configuration.

blalasaadri
  • 5,693
  • 4
  • 37
  • 57
2

You can set the System properties as VM arguments.

If your project is a maven project then you can execute following command while running the test class:

mvn test -Dapp.url="https://stackoverflow.com"

Test class:

public class AppTest  {
@Test
public void testUrl() {
    System.out.println(System.getProperty("app.url"));
    }
}

If you want to run individual test class or method in eclipse then :

1) Go to Run -> Run Configuration

2) On left side select your Test class under the Junit section.

3) do the following :

enter image description here

Michael Lihs
  • 5,664
  • 12
  • 40
  • 67
Joby Wilson Mathews
  • 7,870
  • 2
  • 42
  • 43
2

For Unit Tests, the System variable is not instantiated yet when I do "mvn clean install" because there is no server running the application. So in order to set the System properties, I need to do it in pom.xml. Like so:

<plugin>
    <groupId>org.apache.maven.plugins</groupId>
    <artifactId>maven-surefire-plugin</artifactId>
    <version>2.21.0</version>
    <configuration>
        <systemPropertyVariables>
            <propertyName>propertyValue</propertyName>
            <MY_ENV_VAR>newValue</MY_ENV_VAR>
            <ENV_TARGET>olqa</ENV_TARGET>
            <buildDirectory>${project.build.directory}</buildDirectory>
        </systemPropertyVariables>
    </configuration>
</plugin>
Gene
  • 9,441
  • 1
  • 60
  • 54