FrVaBe FrVaBe - 26 days ago 29
Java Question

Override default Spring-Boot application.properties settings in Junit Test

I have a Spring-Boot application where the default properties are set in an

application.properties
file in the classpath (src/main/resources/application.properties).

I would like to override some default settings in my JUnit test with properties declared in a
test.properties
file (src/test/resources/test.properties)

I usualy have a dedicated Config Class for my Junit Tests, e.g.

package foo.bar.test;

import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;

@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {

}


I first thought that using
@PropertySource("classpath:test.properties")
in the TestConfig class would do the trick, but these properties will not overwrite the application.properties settings (see Spring-Boot Reference Doc - 23. Externalized Configuration).

Then I tried to use
-Dspring.config.location=classpath:test.properties
when invoking the test. That was successful - but I don't want to set this system property for each test execution. Thus I put it in the code

@Configuration
@Import(CoreConfig.class)
@EnableAutoConfiguration
public class TestConfig {

static {
System.setProperty("spring.config.location", "classpath:test.properties");
}

}


which unfortunatly was again not successful.

There must be a simple solution on how to override
application.properties
settings in JUnit tests with
test.properties
that I must have overlooked.

Answer

You can use @TestPropertySource to override values in application.properties. From its javadoc:

test property sources can be used to selectively override properties defined in system and application property sources

For example:

@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public class ExampleApplicationTests {

}
Comments