How to Use @ LocalPort with @ DynamicPropertySource in a SpringBootTest

preview_player
Показать описание
Learn how to dynamically set properties in your Spring Boot tests by combining `@ LocalServerPort` and `@ DynamicPropertySource` in a simple and effective way.
---

Visit these links for original content and any more details, such as alternate solutions, latest updates/developments on topic, comments, revision history etc. For example, the original title of the Question was: How to use @ LocalPort together with @ DynamicPropertySource in a SpringBootTest?

If anything seems off to you, please feel free to write me at vlogize [AT] gmail [DOT] com.
---
Understanding the Problem: Using @ LocalPort with @ DynamicPropertySource

When working with Spring Boot tests, it's common to want to run your application in a random port environment. This is particularly useful for integration testing, allowing you to avoid port conflicts. However, this setup can introduce complexities, especially when you need to access dynamic properties like JWT URL endpoints that rely on the randomly assigned port.

In this guide, we'll address how to use the @ LocalServerPort annotation along with @ DynamicPropertySource within a Spring Boot test, ensuring that your application can dynamically adjust its configurations based on the runtime environment.

The Configuration Context

Let's first look at the essential aspects of our configuration:

Application Configuration Files

[[See Video to Reveal this Text or Code Snippet]]

[[See Video to Reveal this Text or Code Snippet]]

In our local development environment, we have a controller set up to mock OAuth2 endpoints and return self-signed tokens. While everything works seamlessly at a static port configuration, challenges arise when we attempt to switch to a dynamic random port using WebEnvironment.RANDOM_PORT.

The Challenge

The immediate issue is overriding the jwk-set-uri property when running the Spring Boot test on a random port. The @ DynamicPropertySource is designed to facilitate dynamic property changes, but due to the constraints of Kotlin, it needs to reference static properties.

The Solution: Using @ JvmStatic

The key to solving this problem lies in using the @ JvmStatic annotation with the @ LocalServerPort property. By doing so, you can leverage this port within the static context of the @ DynamicPropertySource.

Step-by-Step Implementation

Here's how to effectively implement this solution:

[[See Video to Reveal this Text or Code Snippet]]

Breakdown of the Code

@ LocalServerPort: This annotation injects the port number assigned at runtime into the port variable.

@ JvmStatic: By annotating it with @ JvmStatic, it allows the registerUrl function in the companion object to access the port variable in a static context.

Dynamic Property Registration: The registerUrl function utilizes the DynamicPropertyRegistry to update the JWT endpoint dynamically, ensuring that it points to the correct port.

Final Thoughts

Using @ LocalPort with @ DynamicPropertySource is a powerful way to maintain flexibility in your Spring Boot tests. This synergy allows integration tests to run without manual adjustments, reflecting the dynamic nature of your application configurations.

By adopting the method described, you should now be able to effectively manage environment variables in your Spring Boot tests, no matter the port assigned.

If you have any other questions or need clarification, feel free to reach out!
Рекомендации по теме
welcome to shbcf.ru