Spring Boot Test - Overriding bootstrap.properties Files

We use bootstrap.properties in the Spring Boot application to configure Spring properties related to Cloud Config.

We want to ignore these properties during testing, because we do not want to connect to the configuration server for unit testing. Therefore, we are looking for a way to completely undo properties from the main bootstrap.properties and provide a new one for testing or overriding custom properties.

We tried to create src/test/resources/bootstrap.properties , src/test/resources/bootstrap-test.properties with the property spring.cloud.config.enabled=false , but this did not work.

we tried to install as shown below before running TestClass

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

and it didnโ€™t work.

While the Spring Boot Documentation is pretty good with regards to how application.properties works, I could not even find a single link to bootstrap.properties .

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

+5
source share
2 answers

(answering my own question here)

After many attempts and errors, it turned out that by setting the spring profile to test , he actually selects bootstrap-test.properties and combines it with the main bootstrap.properties file.

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

Hope this helps someone.

+4
source

If you use the @SpringBootTest annotation, you can override the properties in bootstrap.properties with the following parameters:

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

Otherwise, you can:

  • Add @ActiveProfiles('test') to the test class
  • Create a file called bootstrap-test.properties
  • Add the properties you want to overwrite, for example. spring.cloud.config.enabled=false

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

spring.cloud.config.enabled=false

+4
source

Source: https://habr.com/ru/post/1263694/


All Articles