Allow @DynamicPropertySource on non-static fields
See original GitHub issueWhile @DynamicPropertySource
is great, it would be even better if it could be executed on each @Test
method instead only one time at start of the test class.
For example: okhttp MockWebServer
starts on a free random port. But each test needs its own MockWebServer
as otherwise the state is preserved between test runs.
@SpringBootTest
public class MockTest implements BeforeEachCallback {
private MockWebServer mockServer;
//requires a fresh instance for each @Test method
@Override
public void beforeEach(ExtensionContext extensionContext) {
mockServer = new MockWebServer();
}
//this is not possible as mockServer is not static!
@DynamicPropertySource
static void changePort(DynamicPropertyRegistry registry) {
registry.add("my.app.base.url", () -> "http://localhost:" + mockServer.getPort());
}
}
If I’d make the MockWebServer
a static
field, that won’t work if multiple @Test
classes make use of the mock. Eg assertEquals(1, mockServer.getRequestCount());
would only be valid for the first test method, as mockwebserver preserves the state then.
Issue Analytics
- State:
- Created 3 years ago
- Reactions:4
- Comments:6 (4 by maintainers)
Top Results From Across the Web
Guide to @DynamicPropertySource in Spring - Baeldung
Let's suppose we're developing a typical application that uses PostgreSQL as its database. We'll begin with a simple JPA entity:
Read more >How to set MockWebServer port to WebClient in JUnit test?
Update: I came up with the following, which works. But maybe anyone knows how to make the mockserver field non-static? @ContextConfiguration( ...
Read more >spring-projects/spring-boot - Gitter
The problem I have now is that the connectivity data I get via testcontainers is retrieved via instance fields inherited from a @Parameterized...
Read more >DynamicPropertySource (Spring Framework 6.0.2 API)
This annotation and its supporting infrastructure were originally designed to allow properties from Testcontainers based tests to be exposed easily to ...
Read more >Can we make static reference to non-static fields in java?
Since it is not allowed this will generate a compile time error. import java.util.Scanner; public ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
That’s correct. That issue was bulk closed due to lack of interest.
That’s what I had in mind. Your use case reminded me of that test-scoped bean proposal.
And your use case would likely be a candidate for re-opening that issue. So feel free to request that it be reopened if you feel strongly about it, and we can revisit the idea.
Ok, so but testscoped beans have been closed without any implementation?
Is it maybe possible to declare my service that makes use of the “dynamic”
@Value("${my.app.base.url}")
as test-scoped? So that it rereads the dynamic property from application-context?