I've got a
thing=false
In my property file for a Spring Boot project. While running my Junit tests for the project, I want
thing=true
How do I do this?
I've got a
thing=false
In my property file for a Spring Boot project. While running my Junit tests for the project, I want
thing=true
How do I do this?
Just add application.properties
file in:
src -> test -> resources
and set property to desired value.
You can set a particular configuration property for your unit tests using the TestPropertySource
annotation with the properties parameter:
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = AppConfig.class)
@TestPropertySource(properties = {
"thing=true"
})
public class ThingTrueTests {
}
This provides a way to set a configuration property without using a properties/yaml file. However it is a class-level annotation so it will apply to all the tests in that class.
If you have a set of tests that rely on thing being false then put these in one unit test class and put all the tests that rely on thing being true in another unit test class.
Note: If you're writing groovy code then the TestPropertySource annotation would look like this:
@TestPropertySource(properties = [
"thing=true"
])
Spring provides a support for the same use @TestPropertySource
annotation , is a class-level annotation that is used to configure the locations() of properties files and inlined properties() to be added to the Environment's set of PropertySources for an ApplicationContext for integration tests.
Example
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = AppConfig.class)
@TestPropertySource(locations="classpath:test.properties")
public class SampleApplicationTests {
}
in your test.properties present in Junit class path you can override the variable you want mentioned under application.properties