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.

MockWebServer: Clear requests before each test

See original GitHub issue

Can we have a method to clear requests? Something like this:

    @BeforeEach
    fun cleanRequests() {
        mockServer.clearRequests();
    }

My tests are taking requests from previous tests. I wouldn’t want to shutdown entirely

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:11
  • Comments:15 (4 by maintainers)

github_iconTop GitHub Comments

2reactions
nightswimmingscommented, Nov 16, 2021

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 more comfy for that scenario

0reactions
yschimkecommented, Nov 20, 2021

Is it possible the item reading the property is in the wrong scope? Singleton?

https://www.baeldung.com/spring-dynamicpropertysource

If our PostgreSQL container is going to listen to a random port every time, then we should somehow set and change the spring.datasource.url configuration property dynamically. Basically, each test should have its own version of that configuration property.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Running test with MockWebServer always call the failure call ...
I'm running mock web server to test ...
Read more >
Test Spring WebClient with MockWebServer from OkHttp
Write tests for your Spring WebClient usage with OkHttp's MockWebServer and mock HTTP responses to test different scenarios.
Read more >
Clearing & Resetting - MockServer
Clearing & Resetting. MockServer has the following internal state: recorded requests; active expectations; recorded expectations; logs.
Read more >
Integration Testing with MockWebServer - Industrial Logic
It creates a real WebClient, and inserts the randomly generated url from the mockServer into it. Then, before each test, you can insert...
Read more >
MockWebServer.setDispatcher - Java - Tabnine
@Override protected void before() throws Throwable { clear(); server = new MockWebServer(); final Dispatcher dispatcher = new Dispatcher() { @Override ...
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