java - Inject / override a properties value to a Spring Boot properties file in unit/integration tests phase

Java - Inject / override a properties value to a Spring Boot properties file in unit/integration tests phase

In Spring Boot, you can override properties during tests by providing additional property sources. Here's how you can do it:

  1. application.properties: Define your default properties in the src/main/resources/application.properties file.

  2. application-test.properties: Create a separate properties file for your test environment, like src/test/resources/application-test.properties, where you can override properties specifically for tests.

  3. Use @TestPropertySource annotation: In your JUnit tests, you can use the @TestPropertySource annotation to specify the location of the test properties file.

Here's an example:

import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.TestPropertySource; import static org.junit.jupiter.api.Assertions.assertEquals; @SpringBootTest @TestPropertySource(locations = "classpath:application-test.properties") public class MyServiceTest { @Autowired private MyService myService; @Test public void testPropertyOverride() { String overriddenValue = myService.getProperty(); assertEquals("test-value", overriddenValue); } } 

In this example:

  • MyService is a Spring-managed bean with a method getProperty() that retrieves a property value.
  • application-test.properties contains property overrides specific to the test environment, for example:
    my.property=test-value 

By annotating the test class with @TestPropertySource, you specify that Spring Boot should use the properties defined in application-test.properties during the test execution.

This approach allows you to have different property configurations for your main application and your tests, ensuring that your tests run in a controlled environment.

Examples

  1. How to inject properties value in Spring Boot unit tests? Description: This query seeks methods to inject or override property values specifically for unit testing in a Spring Boot application.

    @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(locations = "classpath:test.properties") public class MyUnitTest { @Value("${my.property}") private String myProperty; @Test public void testPropertyInjection() { assertThat(myProperty).isEqualTo("test-value"); } } 
  2. Spring Boot test property injection example Description: Demonstrates a simple example of injecting properties in Spring Boot unit tests using @TestPropertySource.

    @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(properties = "my.property=test-value") public class MyUnitTest { @Value("${my.property}") private String myProperty; @Test public void testPropertyInjection() { assertThat(myProperty).isEqualTo("test-value"); } } 
  3. Override Spring Boot properties for integration tests Description: Shows techniques to override Spring Boot properties specifically for integration tests to facilitate different configurations.

    @SpringBootTest @ActiveProfiles("integration-test") public class MyIntegrationTest { @Value("${my.property}") private String myProperty; @Test public void testPropertyOverride() { assertThat(myProperty).isEqualTo("test-value"); } } 
  4. How to mock properties in Spring Boot tests? Description: This query pertains to mocking property values in Spring Boot tests, often used to isolate components for testing purposes.

    @RunWith(SpringRunner.class) @SpringBootTest public class MyUnitTest { @Value("${my.property}") private String myProperty; @MockBean private MyService myService; @Before public void setUp() { Mockito.when(myService.getProperty()).thenReturn("mocked-value"); } @Test public void testMockedProperty() { assertThat(myProperty).isEqualTo("mocked-value"); } } 
  5. Spring Boot test properties override example Description: Illustrates overriding properties in Spring Boot tests using @TestPropertySource.

    @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(properties = "my.property=override-value") public class MyUnitTest { @Value("${my.property}") private String myProperty; @Test public void testPropertyOverride() { assertThat(myProperty).isEqualTo("override-value"); } } 
  6. Injecting custom properties in Spring Boot tests Description: Explains how to inject custom properties specifically for Spring Boot tests to control configurations.

    @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(properties = "custom.property=custom-value") public class MyUnitTest { @Value("${custom.property}") private String customProperty; @Test public void testCustomPropertyInjection() { assertThat(customProperty).isEqualTo("custom-value"); } } 
  7. How to override properties file in Spring Boot tests? Description: Addresses methods for overriding entire properties files in Spring Boot tests to provide tailored configurations.

    @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(locations = "classpath:test.properties") public class MyUnitTest { @Value("${my.property}") private String myProperty; @Test public void testPropertyOverride() { assertThat(myProperty).isEqualTo("test-value"); } } 
  8. Spring Boot property placeholder test example Description: Provides an example of testing Spring Boot applications with property placeholders to ensure proper property resolution.

    @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(properties = "my.property=${test.value}") public class MyUnitTest { @Value("${my.property}") private String myProperty; @Test public void testPropertyPlaceholder() { assertThat(myProperty).isEqualTo("resolved-value"); } } 
  9. How to set properties for Spring Boot test environment? Description: Offers techniques for setting up properties specifically tailored for Spring Boot test environments.

    @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(properties = "test.property=test-value") public class MyUnitTest { @Value("${test.property}") private String testProperty; @Test public void testCustomProperty() { assertThat(testProperty).isEqualTo("test-value"); } } 
  10. Spring Boot test properties file location Description: Guides on specifying the location of properties files for Spring Boot tests to facilitate property injection.

    @RunWith(SpringRunner.class) @SpringBootTest @TestPropertySource(locations = "classpath:test.properties") public class MyUnitTest { @Value("${my.property}") private String myProperty; @Test public void testPropertyFileLocation() { assertThat(myProperty).isEqualTo("test-value"); } } 

More Tags

ansible taskmanager rendering database-deadlocks rake-task android-tablayout unzip semantic-ui alfresco jquery-ui-sortable

More Programming Questions

More Fitness Calculators

More Trees & Forestry Calculators

More Physical chemistry Calculators

More Housing Building Calculators