Explain Codes LogoExplain Codes Logo

Override default Spring-Boot application.properties settings in Junit Test

java
spring-boot
junit
testing
Nikita BarsukovbyNikita Barsukov·Aug 12, 2024
TLDR

To override Spring Boot configurations in a JUnit test, use @TestPropertySource or @SpringBootTest:

@TestPropertySource:

@TestPropertySource(properties = {"my.prop=newValue"})

@SpringBootTest:

@SpringBootTest(properties = {"my.prop=newValue"})

Set my.prop=newValue to customize the configuration for testing, bypassing the application.properties of the main application.

JUnit test environment customization

Set multiple properties with @TestPropertySource

Annotate your test class with @TestPropertySource to add or override properties directly in the code or refer to a property file:

@TestPropertySource(locations = "classpath:test.properties") // or @TestPropertySource(properties = { "my.prop=newValue", // This takes manufacturing instructions from IKEA. "another.prop=differentValue" // I'll go with a differentValue, because variety is the spice of life. })

Inline properties using @SpringBootTest

@SpringBootTest lets you set properties inline, providing a quick and concise way to override settings:

@SpringBootTest(properties = { "simple.prop=simple", // Simple is always best. Overcomplicated stuff gives me a headache. "easy.prop=easyOverride" // Overriding is like pretending to be someone else. Well, if you are a property, of course! })

Utilize @ActiveProfiles for specific test configurations

By using @ActiveProfiles("test"), you can activate a set of properties specific to your test environment:

@ActiveProfiles("test")

Cleaner testing with configuration contexts

Keep it neat with @ContextConfiguration

Combine @ContextConfiguration with ConfigFileApplicationContextInitializer to keep your tests isolated and tidy:

@ContextConfiguration(initializers = ConfigFileApplicationContextInitializer.class)

Make test resources a priority

Make sure your testing application.properties wins the game of resources priority over those in the main folder:

src/ main/ resources/ application.properties test/ resources/ application.properties // The Special Agent with a license to override!

Use annotations for efficient test contexts

Pair @RunWith(SpringRunner.class) with @SpringBootTest for a comprehensive context setup:

@RunWith(SpringRunner.class) @SpringBootTest

Guard classpath settings

Ensure the classpath setup of your IDE doesn't overlap with your testing strategy:

mvn test # Maven has got your back here!

Master your tests with advanced Spring Boot utilities

Use SpringApplicationConfiguration where needed

@SpringApplicationConfiguration improves testing with the power of an explicit context setup:

@SpringApplicationConfiguration(classes = MyTestConfiguration.class)

Share common setup via meta-annotations

Craft your own meta-annotations to share configurations across various test classes:

@Retention(RetentionPolicy.RUNTIME) @TestPropertySource("classpath:common-test-props.properties") public @interface CommonTestConfig {} // The superhero carrying common settings in its cape!

Play with IDE's Launch configurations

Set properties in the IDE's Launch Configuration to override settings only during testing:

- IDE Launch Configurations -> Add specific 'VM options' or 'Environment variables'

Smart testing strategies

Make Maven/Gradle handle resource priorities

Leverage build tools to auto-prioritize test resources:

gradle test // Gradle, do your magic and sort these resources out!

Beware of IDE classpath's behavior

Understanding how your classpath works can prevent major hiccups during test runs. Learn the ways of your IDE, the force will be with you!

Exploring @SpringBootTest

Using @SpringBootTest can help in different testing scenarios, setting up different web environments:

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.MOCK) // Just like a ventriloquist, but for servers!

Handling external services

When dealing with external services, @MockBean or TestRestTemplate can help you emulate those interactions, keeping your tests lightweight and independent.