Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot test - overriding bootstrap.properties

We are using bootstrap.properties in a Spring Boot app to configure Spring Cloud Config related properties.

We want to ignore these properties during the testing as we don't want to connect to config server for unit testing. So we are looking for a way to completely undo properties from main bootstrap.properties and provide a new one for testing or override selective properties.

We tried with creating src/test/resources/bootstrap.properties, src/test/resources/bootstrap-test.properties with spring.cloud.config.enabled=false property but it didn't work.

we tried to set as below before starting the TestClass

static {
    System.setProperty("spring.cloud.config.enabled", "false");
}

and it didn't work.

While Spring Boot documentation is pretty good about how application.properties works, I couldn't find even one reference to bootstrap.properties.

Any help is much appreciated on a reliable way to override bootstrap.properties during testing.

like image 724
singularity Avatar asked Feb 01 '17 16:02

singularity


People also ask

How do I override a properties file in Spring boot?

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.

Does bootstrap override application properties?

The two contexts share an Environment , which is the source of external properties for any Spring application. By default, bootstrap properties (not bootstrap. properties but properties that are loaded during the bootstrap phase) are added with high precedence, so they cannot be overridden by local configuration.


2 Answers

If you are using the @SpringBootTest annotation you can override properties in the bootstrap.properties with the following:

@SpringBootTest(properties = "spring.cloud.config.enabled=false")

Otherwise, you can:

  1. Add @ActiveProfiles('test') to your test class
  2. Create a file named bootstrap-test.properties
  3. Add the properties you want to overwrite e.g. spring.cloud.config.enabled=false

Update: If you want to disable spring cloud config for all tests, you can simply create a bootstrap.properties inside your test/resources folder with the following property:

spring.cloud.config.enabled=false

like image 144
Doug Avatar answered Sep 18 '22 09:09

Doug


(answering my own question here)

After lots of try and error found that by setting the spring profile to test it actually picks the bootstrap-test.properties and combines that with main bootstrap.properties file.

In this case, setting spring.cloud.config.enabled=false was still trying to bootstrap as in the main bootstrap it was set to spring.cloud.config.server.bootstrap = true so we had to set this property to false in bootstrap-test.properties to inactive cloud server completely.

Hope this helps somebody.

like image 32
singularity Avatar answered Sep 18 '22 09:09

singularity