How do I mock a REST template exchange?
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When production code calls RestTemplate.exchange, a good test should avoid real network traffic while still proving that your client handles requests and responses correctly. In practice, there are two main strategies: mock RestTemplate directly with Mockito, or keep a real RestTemplate and attach MockRestServiceServer to it.
Strategy 1: Mock RestTemplate With Mockito
If your class receives RestTemplate through constructor injection, Mockito is usually the fastest option. You stub the exchange call and return a controlled ResponseEntity.
Here is a simple client:
And the corresponding unit test:
This style is ideal when the test cares mainly about your client logic rather than the internals of RestTemplate itself.
Strategy 2: Use MockRestServiceServer
If you want to verify the actual request URL, HTTP method, and response handling against a real RestTemplate, MockRestServiceServer is usually the better tool.
This approach stays closer to real HTTP client behavior without leaving the JVM.
Why Dependency Injection Matters
Mocking is much easier when RestTemplate is injected into the class under test. If the class creates new RestTemplate() internally, the test has to work much harder to control behavior.
That is why a client like this is easier to test and maintain:
Good testability often starts with small constructor design decisions.
Watch the Exact exchange Overload
exchange has several overloads. Your stub or mock-server expectation must match the one used by production code. This becomes especially important when the code uses:
- '
ParameterizedTypeReference,' - URI templates with variables,
- custom request entities,
- generic response bodies.
A mock that targets the wrong overload can look correct and still never be called.
Which Tool Should You Choose?
Use Mockito when you want a narrow unit test of your class behavior. Use MockRestServiceServer when request construction itself is part of what you want to validate.
Neither approach is universally better. They answer slightly different testing questions.
Common Pitfalls
A common mistake is mocking one exchange overload while the real code calls another. That produces confusing tests that either fail unexpectedly or pass for the wrong reason.
Another issue is instantiating RestTemplate directly inside the class under test. That makes the code harder to isolate and often encourages weaker tests.
Teams also forget server.verify() when using MockRestServiceServer, which means the test may not actually assert that the expected HTTP interaction occurred.
Summary
- '
RestTemplate.exchangecan be tested either by mockingRestTemplateor by usingMockRestServiceServer.' - Mockito is best for narrow unit tests focused on service logic.
- '
MockRestServiceServeris better when request details should be validated.' - Constructor injection makes HTTP clients much easier to test.
- Always match the exact
exchangeoverload used by the production code.

