jeudi 16 avril 2015

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 Tets, 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 tought that using @PropertySource("classpath:test.properties") in the TestConfig class could do the trick, but these properties will not overwrite the application.properties settings (see Spring-Boot Reference Doc - 23. Externalized Configuration).


Than I tried to use -Dspring.config.location=classpath:test.properties when invoking the test. That was successful - but I don't like to set this system property for each test execution. Thus I put it in 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.


I am grateful for any help!


Kind regards Franz


Aucun commentaire:

Enregistrer un commentaire