question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Allow @DynamicPropertySource on non-static fields

See original GitHub issue

While @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:open
  • Created 3 years ago
  • Reactions:4
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
sbrannencommented, Apr 7, 2020

Ok, so but testscoped beans have been closed without any implementation?

That’s correct. That issue was bulk closed due to lack of interest.

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?

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.

0reactions
membersoundcommented, Apr 7, 2020

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?

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found