New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
MockWebServer: Clear requests before each test #6212
Comments
Are you using the test rule? There should be a new instance for every test. |
Yes, I'm using |
Can you switch to PER_METHOD? Assuming such a thing exists. |
Yeah, MockWebServer instances are not expected to be reused. |
Will a clear method be enough to be reusable ? I can open PR, why not I guess ? |
No, I don't think it's a good fit for a PR. The clear method encourages a pattern we don't support well. |
@JakeWharton @swankjesse What if we need an static MockWebServer in order to register @DynamicPropertySource properties with the mockWebServer.getPort()? In that case we need a single instance and resetting it, right? |
Can't you change your logic to update the DynamicPropertyRegistry as each test runs with the new port/url? |
DynamicPropertyRegistry method must be static as per documentation, so I guess it does not reevaluate within the class. I think that would imply restarting the whole spring context since the mechanism is similar to that of @SpringBootTest(properties=X). So we'd need the mockrestserver instance to be static as well, I am not sure it makes sense to reinstantiate it globally and start/shutwoning on each test, I find the clearRequests() approach |
The method is static, but it provides a Supplier that gets called on demand. I'm not saying it's ideal for you, just that it doesn't appear that you are blocked as you could update the port each time a new MockWebServer is started for your test. |
I don't understand the Supplier part but I am interested.. :) I have this currently, how would you exactly do it?
|
Maybe something like the following, where port should be read everytime the property is accessed.
|
Ahh I think I understand you. @DynamicPropertySource is only evaluated once, but by your words I understand that the property supplier itself does it every time the property is accessed. If so I guess your example is missing a port = mockWebServer.getPort() at the end of the before(), right? |
@yschimke I did some tests and the evaluation for the supplier is done once at context startup and that occurs before the BeforeEach, so even if we managed to tell spring to reevaluate whole context before each test, I should make sure that the BeforeEach is still executed before the property evaluation, otherwise I need to do an ugly hack for reinitializing the mockwebserver on the supplier only as long as it takes part during the same test. Frankly I still see clearRequest() a much nicer approach (and more performant) |
Is it possible the item reading the property is in the wrong scope? Singleton? https://www.baeldung.com/spring-dynamicpropertysource
|
May I ask if there is any solution here? Is okttp Mockserver just unusable with Spring tests and their lifecycle when a @DynamicPropertiesSource is used in combination with a static instance? I have the same problem as stated above and I actually don't understand why the queue can't just be cleared via API. |
Sharing state between tests is problematic, especially since MockWebServer isn’t dynamic on features like whether TLS is enabled. The ‘more performant’ argument is a rationalization, not a reason; MockWebServer performance is completely reasonable without reusing instances. I’m reopening this to find a way to use MockWebServer despite Spring Boot’s limitations. It might be something as basic as providing a factory of MockWebServer instances. |
Can we have a method to clear requests? Something like this:
My tests are taking requests from previous tests. I wouldn't want to shutdown entirely
The text was updated successfully, but these errors were encountered: