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.
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.
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.
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:
@ActiveProfiles('test')
to your test classbootstrap-test.properties
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
(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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With