Override default Spring-Boot application.properties settings in Junit Test
You can use @TestPropertySource
to override values in application.properties
. From its javadoc:
test property sources can be used to selectively override properties defined in system and application property sources
For example:
@RunWith(SpringJUnit4ClassRunner.class)
@SpringApplicationConfiguration(classes = ExampleApplication.class)
@TestPropertySource(locations="classpath:test.properties")
public class ExampleApplicationTests {
}
Spring Boot: How to override default properties in Unit tests
You can use @ActiveProfiles("test")
. This will set the application-test.yml
properties into test environment.
@RunWith(SpringRunner.class)
@SpringBootTest
@ActiveProfiles("test")
public class TestconfigApplicationTests {
...
}
If we need to target different environments, there’s a build-in mechanism for that in Boot so there is no need for additional libraries or refactorings.
We can simply define an application-environment.properties
file in the src/main/resources
directory – and then set a Spring profile with the same environment name.
For example, if we define a staging
or test
environment, that means we’ll have to define a staging or test profile and then application-staging.properties
or application-test.properties
.
This env
file will be loaded and will take precedence over the default property file which is application.properties
. Note that the default file will still be loaded, it’s just that when there is a property collision the environment specific property file takes precedence, meaning the properties specified in application-staging.properties
or application-test.properties
will override the ones in application.properties
.
Each test class uses its own profile, so you need to specify the active profile for each class.
One additional thing that might be of interest for you is that you can mock services via configuration
classes
@Configuration
@Profile("mockEntityService")
public class EntityServiceMockProvider {
@Bean
@Primary
public EntityService entityService() {
EntityService mockedEntityService = Mockito.mock(EntityService.class);
Entity entity= Mockito.mock(Entity.class);
when(mockedEntityService.save(any(Entity.class)))
.thenReturn(entity);
return mockedEntityService ;
}
}
In test classes you can use multiple active profiles:
e.g. @ActiveProfiles({"test", "mockEntityService"})
so instead of using the real implementation of the EntityService
you will use the mocked implementation.
Override default spring boot application.properties with application.properties through cmd dynamically
There's a couple ways of doing this, but one way I can think of is to use externalized application properties file. See https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-external-config-files for more specific details.
You would have your application.properties
(under src/main/resources
) with the default property cronExpression
. But when you invoke the JAR, you could pass in a system property to tell Spring where an additional properties file is with possible overrides for that cronExpression
property.
java -jar <jar_name> -Dspring.config.additional-location=my-other-file.properties
Spring Test : why cannot access application.properties?
If you also add a application.properties
file inside src/test/resources
, this will "override" the one in src/main/resources
, and hence none of the keys you define in your "production" properties file will be seen.
So either remove your application.properties
in src/test/resources
to use your property file in src/main/resources
or define your values there.
Add environment property to @SpringBootTest before application.properties evaluation?
You can use Spring Framework's @DynamicPropertySource
for this purpose. It's described in this blog post.
In the case of MyWebTest
, the dynamic property source would look something like this:
@DynamicPropertySource
static void mockPortProperty(DynamicPropertyRegistry registry) {
registry.add("mockPort", () -> PORT);
}
Related Topics
What Does the "Assert" Keyword Do
How to Make a Java Class That Implements One Interface with Two Generic Types
Removing Invalid Xml Characters from a String in Java
Import Xxx Cannot Be Resolved for Java Se Standard Classes
Java, "Variable Name" Cannot Be Resolved to a Variable
Difference Between Arrays.Aslist(Array) and New Arraylist<Integer>(Arrays.Aslist(Array))
Securing a Password in a Properties File
How to Resolve Unable to Load Authentication Plugin 'Caching_Sha2_Password' Issue
Does Calling Clone() on an Array Also Clone Its Contents
How to Retrieve Image from Project Folder
Mockito:How to Verify Method Was Called on an Object Created Within a Method
Java Linkedhashmap Get First or Last Entry
Declaring and Initializing Variables Within Java Switches
How to Insert Image into Jtable Cell