Skip to content

Make integration testing of webclient easier with @MockServerResponse annotation. #45652

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

Closed
MatejNedic opened this issue May 23, 2025 · 3 comments
Labels
for: external-project For an external project and not something we can fix status: declined A suggestion or change that we don't feel we should currently apply

Comments

@MatejNedic
Copy link
Contributor

MatejNedic commented May 23, 2025

When doing full blown integration tests with @SpringBootTest and having webclient reach to 3rd party services instead of mocking it or injecting MockServerClient client and mocking MockignServerContainer support @MockServerResponse annotation should support container mock out of the box.

When Testcontainer of type MockServer is created and annotated with @ServiceConnection.

We can annotate test class

@MockServerResponse(endpoint = "endpoint we are hitting with webclient",
 queryParameters ="optional",  method = "POST", response = "resources/3rdPartyResponse.json")
@Test
void test3rdPartyIntegration () {
// Our custom logic
}

In background MockServer container will be mocked with mockServerClient for endpoint and method specified in annotation and with given response out of the box. This way we remove a lot of boilerplate code for simpler integrations which would be written with mockServerClient, also we are making test more readable by clearly extracting response in an annotation.

If this is something you think is good idea I am willing to contribute. :)

@MatejNedic MatejNedic changed the title Make integration testing of webclient easier with @MockResponse annotation. Make integration testing of webclient easier with @MockServerResponse annotation. May 23, 2025
@spring-projects-issues spring-projects-issues added the status: waiting-for-triage An issue we've not yet triaged label May 23, 2025
@wilkinsona
Copy link
Member

wilkinsona commented May 23, 2025

Thanks for the suggestion.

I'm not convinced that involving Docker is worth it here. It's generally most beneficial when you want to test your application against a real running service rather than a mocked or in-memory alternative that may not be exactly equivalent.

For testing code that uses WebClient (or RestClient), we generally recommend something like OkHttp MockWebServer instead.

@wilkinsona wilkinsona added the status: waiting-for-feedback We need additional information before we can continue label May 23, 2025
@MatejNedic
Copy link
Contributor Author

Hey thanks on fast response.

I usually do what you described even put second service in network mode with dependency it uses (database, kafka whatever it requires).

Problem here is I want response from 3rd party service which I don't have an access to, so I cant spin image.

I want to do proper integration test which means lifting context.

Both okhttp and mockservercontainer will do same, one will start container, other will start the server without container.

Annotation could work for both right?

I am not testing what I am sending since unit tests are covering business logic (various use cases with linked tickets). Integration tests with 3rd party whatever I do is a mock. Even sometimes if you assert what you send doesn't matter since docs can be different than api behaviour (so sys test is only way).

I fully understand what you mean but I think okhttp could benefit of the same annotation?

Also I see a lot of cases which this annotation wouldn't help, for example if you want to assert a headers or something specific.

If you don't see benefits I can close the ticket :)

@spring-projects-issues spring-projects-issues added status: feedback-provided Feedback has been provided and removed status: waiting-for-feedback We need additional information before we can continue labels May 23, 2025
@bclozel
Copy link
Member

bclozel commented May 24, 2025

Closing this issue in favor of spring-projects/spring-framework#34892 where we are going to better document how to test clients. If we see new candidates features for Boot there, we can reconsider.

Thanks!

@bclozel bclozel closed this as not planned Won't fix, can't repro, duplicate, stale May 24, 2025
@bclozel bclozel added status: declined A suggestion or change that we don't feel we should currently apply for: external-project For an external project and not something we can fix and removed status: waiting-for-triage An issue we've not yet triaged status: feedback-provided Feedback has been provided labels May 24, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
for: external-project For an external project and not something we can fix status: declined A suggestion or change that we don't feel we should currently apply
Projects
None yet
Development

No branches or pull requests

4 participants