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. :)
Comment From: wilkinsona
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.
Comment From: MatejNedic
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 :)