20

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.

3 Answers 3

30

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

2
  • 3
    nice answer @Doug. Just specifying @ActiveProfiles('test') and providing an application-test.yml / application-test.properties will work.
    – maggu
    Commented Jun 22, 2018 at 16:18
  • 5
    no, it doesn't with SB 2.4.0+, as long you have bootstrap it will be downloaded first always Commented Jan 19, 2021 at 21:48
10

(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.

4
  • 1
    Does not work for me. By default, bootstrap-test.properties is ignored. I had to add @TestPropertySource(locations="classpath:bootstrap-test.properties") to my @SpringBootTest class. Using Spring Boot Starter 1.5.3.RELEASE.
    – user909481
    Commented Jun 21, 2017 at 8:00
  • I did the following: modified build.gradle to specify the spring profile for unit test like this: test { systemProperties System.properties systemProperties.put("spring.profiles.active","unittest") } created bootstrap-unittest.yml and placed it into src/test/resources. It is configured with all of the spring.cloud enabled properties to false.
    – toddcscar
    Commented Oct 18, 2017 at 17:00
  • @user909481 bootstrap-test.properties will only be picked up automatically if your test has @ActiveProfiles('test'). If you need to disable it for all tests, please see my answer above
    – Doug
    Commented Nov 24, 2017 at 11:35
  • 1
    @toddcscar that works, but if you want to disable for all tests, you don't even need to set a profile if you don't want to. (see my answer above)
    – Doug
    Commented Nov 24, 2017 at 11:36
0

For manual testing, I have added local application.yml and bootstrap.yml files to the root of my working directory. The application.yml contains a single line:

spring.cloud.bootstrap.location: file:.

This setting activates the local bootstrap.yml file in the working directory and disables the one on the classpath. The idea came from Spring Boot 2 #466

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.