Skip to content
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

Replace OkHttp mockwebserver by Spring mocking solution #62

Draft
wants to merge 14 commits into
base: main
Choose a base branch
from

Conversation

Tristan-WorkGH
Copy link
Contributor

There is actually multiple problems observed:

  • the tests are quite slow for what we want to test (want to check correct http calls are done)
  • the part generating http responses is difficult to read and non-optimized (used matches(regexp) for constant path)
  • we can't control as is exactly which test do what HTTP calls
  • all HTTP requests are made on the same server, we don't check which Spring service contact which server

So now:

  • passing from a mock-server to mocking rest client
    • using Spring Test solution: MockRestServiceServer
    • inject RestTemplateBuilder in service to easily plugin during tests
    • multiples MockRestServiceServer to have fine grained tests on each services (and also because @RestClientTest support only 1 RestTemplate (good to test only one service) ...)
    • each MockRestServiceServer build with MockServerRestTemplateCustomizer and RestTemplateBuilder
  • spitted huge dispatch() block in small atomic expectations+response
  • each test specify what calls is expected to be produced and check exactly/only them are made
  • also fixed usage of UriComponentsBuilder for query params
  • Tests duration pass from 29 tests in ~3s to 38 tests in ~1s.

For the discussion about using a mock-server or not:

There is mainly two way to test HTTP calls:

  • starting an HTTP server controlled by the tests (mock-server)
  • mocking or replacing the http class use by services (mock class/functions)
    • It is for integration (or unit?) test (quick)
    • libraries like Mockito can be used
    • Spring offer MockRestServiceServer for mocking RestTemplate directly
    • ...

There is also the point of whether using blocking RestTemplate or non-blocking WebClient.

@Tristan-WorkGH Tristan-WorkGH added the enhancement New feature or request label Sep 22, 2023
@Tristan-WorkGH Tristan-WorkGH self-assigned this Sep 22, 2023
@Tristan-WorkGH
Copy link
Contributor Author

Merge blocker because waiting Spring-Boot 3 migration

@sonarqubecloud
Copy link

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

100.0% 100.0% Coverage
0.0% 0.0% Duplication

@SlimaneAmar SlimaneAmar removed their request for review September 18, 2024 07:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant