Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring-boot application-test.properties

I am trying to unit test the spring-boot application using junit. I have placed the application-test.properties under src/test/resources. I have a ApplicationConfiguration Class which reads the application.properties.

My test class looks like this

@RunWith(SpringRunner.class)
@SpringBootTest(classes=ApplicationConfiguration.class)
@TestPropertySource(locations = "classpath:application-test.properties")
@ActiveProfiles("test")
   public class TestBuilders {
      @Autowired
      private ApplicationConfiguration properties;

When I try to read the properties, it is always null.

My ApplicationConfiguration Class looks something like this

@Configuration
@ConfigurationProperties
@PropertySources({
    @PropertySource("classpath:application.properties"),
    @PropertySource(value="file:config.properties", ignoreResourceNotFound = 
        true)})
public class ApplicationConfiguration{
    private xxxxx;
    private yyyyy;

I tried all possible ways that I found on google.. No luck. Please help! Thanks in Advance.

like image 875
FunWithJava Avatar asked Jan 05 '18 10:01

FunWithJava


People also ask

How do I check Spring boot application properties?

To see all properties in your Spring Boot application, enable the Actuator endpoint called env . This enables an HTTP endpoint which shows all the properties of your application's environment. You can do this by setting the property management.

Where does Spring boot look for property file in test?

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

How does Spring Boot read properties file in test class?

You can use @TestPropertySource annotation in your test class. Just annotate @TestPropertySource("classpath:config/mailing. properties") on your test class. You should be able to read out the property for example with the @Value annotation.

How do I bypass Spring boot application properties?

To override your Spring Boot application properties when it's running on Kubernetes, just set environment variables on the container. To set an environment variable on a container, first, initialise a ConfigMap containing the environment variables that you want to override.


1 Answers

The issue is you don't have @EnableConfigurationProperties on your test class.
When you load the application it start from main class(one which has @SpringBootApplication) where you might have @EnableConfigurationProperties and hence it works when you start the application.
Whereas when you are running the Test with only ApplicationConfiguration class as you have specified here

@SpringBootTest(classes=ApplicationConfiguration.class)

Spring doesn't know that it has to Enable Configuration Properties and hence the fields are not injecting and hence null. But spring is reading your application-test.properties file. This can be confirmed by just injecting the value directly in your test class

@Value("${xxxxx}")
private String xxxxx;

Here the value is injected. But to inject into a class with ConfigurationProperties you need to enable it using @EnableConfigurationProperties

Put @EnableConfigurationProperties on your test class and everythhing works fine.

like image 143
pvpkiran Avatar answered Oct 09 '22 13:10

pvpkiran