Override single entry in application.yml for all tests - spring-boot

Within the SpringBoot application you can provide the configuration via src/main/resource/application.yml.
One single entry should be overriden by the tests (see How to mock Eureka when doing Integration Tests in Spring? ). I tried to provide a test configuration with src/integration-test/resource/application.yml but it overrides the complete configuration.
eureka:
client:
enabled: false
How can I modify one entry of the configuration file for all tests?

Create a application-test.yml in src/main/resource/ with your desired configuration (eureka.client.enabled=false) and also other configurations that you require for your application to start up,
Once your application-test.yml is complete You just need to add following annotation with desired value to your test class ,
#SpringBootTest(value={"spring.profiles.active=test"})
Here spring.profiles.active=test because we have set its value to test because we want to read configurations from application-test.yml.
Hope it helps !

Related

Different YAML configuration file for junit test using an Externalized configuration in Spring Boot

I am following a tutorial on using external configuration files for Spring Boot. I got everything to work exactly as intended but I'm having issues overriding the default YAML config for my tests.
Could someone please point me in the right direction or advice if using '#PropertySource' is the best way to load config files into the project (There is a bunch of properties and I would like to keep the application.yaml as clean as possible)
Project Structure:
src: - main/resources/foo.yml <-- always loads this one
- test/resources/foo.yml <-- never loads
What I tried:
#PropertySource(value = "classpath:foo.yml")
Doesn't load test/resoruces/foo.yml to the classpath
ActiveProfiles()
How I usually change config properties but in this case, it's not a profile so it doesn't work.
Details:
Spring boot: 2.2.7.RELEASE
Try this:
#TestPropertySource(properties = { "spring.config.location=classpath:foo.yml" })

SpringBootTest: how to use application-test.yaml and inherit missing values from application.yaml

I am trying to run an integration test using the annotations :
#RunWith(SpringRunner.class)
#SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
#ActiveProfiles({ "test" }).
The problem I have is that the application loads by using the spring profile "test"
I have two config files:
application.yaml
application-test.yaml
application.yaml contains:
xyz:
list:
- class-name: com.any.prod.ClassName1
jndi-name: com/ws/ClassName1
- class-name: com.any.prod.ClassName2
jndi-name: com/ws/ClassName2
and the other file doesn't contain these values.
these valuer are used by a library that i use, and not directly by my application.
when I load the test with the "test" profile enabled the values from application.yaml are not picked up. If I add the same values to application-test.yaml they are picked up during the binding process.
These are the properties that are picked up:
xyz.list[0].class-name: com.any.prod.ClassName1 (loaded from application-test.yaml)
xyz.list[0].jndi-name: com/ws/ClassName1 (loaded from application-test.yaml)
xyz.list[1].class-name: com.any.prod.ClassName2 (loaded from application-test.yaml)
xyz.list[1].jndi-name: com/ws/ClassName2 (loaded from application-test.yaml)
xyz.list (loaded from application.yaml)
Unfortunately the last entry makes the validation of the properties fail.
Does anyone has in mind what can I do to solve this mystery?
At this point my understanding of how configuration yaml files are wrong (at least for the testing case - inheritance appears to be working just fine when we deploy our app)
You can achieve this by using the annotation #TestPropertySource .

custom properties in junit test, something like application.setDefaultProperties

I'm creating unit/integration test in Spring boot app and have difficulty to add test properties.
this is how main application get properties from src/main/resources/config.json file.
Config config = new AppConfig().config();
if (config == null) {
System.exit(1);
}
application.setDefaultProperties(config.getSpringProperties());
application.run(args);
I'd like to put test config file under src/text/resources/ and use it for integration test with MockMvc. Is there any way to do it?
config.getSpringProperties()
returns Map
Spring boot automatically resolves test properties from src/test/resources/application.properties
Update:
You can define custom properties locations with annotations like #TestPropertySource or #SpringBootTest(properties
Here some resource on the topic:
https://www.baeldung.com/properties-with-spring
Old:
Just add a file named application-test.properties or application-test.yml and annotate the test with #ActiveProfiles("test").
By this you can even have multiple profiles. Just add the #ActiveProfiles("xyz") and name the application properties file application-xyz.yml

Passing a list to an environment variable to exclude certain classes from auto configuration

In Spring you can exclude certain classes from autoconfiguration by defining them in the spring.autoconfigure.exclude property. In my case, we are using yaml to define to exclude certain classes:
spring:
autoconfigure:
exclude: |
org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricsDropwizardAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricsChannelAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.MetricExportAutoConfiguration,
org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration
to exclude the actuator setup. However, as that only needs to happen in certain environments, I want to externalize this setup and pass it as an environment variable. Spring allows you to pass configuration as an environment variable of the form SPRING_AUTOCONFIGURE_EXCLUDE, but how would I pass the list in this case? I could not find anything in Springs documentation on externalized configuration that gave me the answer.
You can do it as follows:
export SPRING_AUTOCONFIGURE_EXCLUDE=org.springframework.boot.actuate.autoconfigure.MetricFilterAutoConfiguration,org.springframework.boot.actuate.autoconfigure.MetricRepositoryAutoConfiguration,org.springframework.boot.actuate.autoconfigure.MetricsDropwizardAutoConfiguration,org.springframework.boot.actuate.autoconfigure.MetricsChannelAutoConfiguration,org.springframework.boot.actuate.autoconfigure.MetricExportAutoConfiguration,org.springframework.boot.actuate.autoconfigure.PublicMetricsAutoConfiguration

Execute the same #SpringBootTest with different properties

I have a #SpringBootTest which is used to execute an integration test on the server. Depending on the configuration I want the server to behave differently. The configuration itself is read by beans (scope = singleton) deep inside my app logic and they read the property via #Value annotation.
How could I execute the same test with different configuration settings? I have tried to write different test classes and annotate them with #TestPropertySource(properties = XYZ). But it seems that this affects all other tests as well (due to the singleton scope?). It there a way to reset the properties after the test?
To respecify my problem: I want to configure my bean with a different #Value property during my tests and this value should only be valid throughout this specific test execution.
Thanks ahead for any pointers.
I have a webservice, which is connecting to the client of other webservice by using property from the config. As in any organization, we have different environments. For testing, I wanted to hit testing env instead of local. This is how I override the default property value only for integration test. By doing this, I can hit the test env instead of default local env.
#SpringBootTest(value = {"eureka.client.enabled=false", // Don't start Eureka
"com.somepackage.webservicename.client.serviceUrl = http://nodename.envname:26730"})
Hope this helps!

Resources