How do I mock a REST template exchange?
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.
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.
Related reading
- How do I modify proxy responses using asynchronous servlets?
- How do I pass arguments to AWS Lambda functions using GET requests?
- How do I plot a Keras/Tensorflow subclassing API model?
- How do I pull from a Git repository through an HTTP proxy?
- How do I monitor the computer''s CPU, memory, and disk usage in Java?
- How do I parse command line arguments in Java?
- How do I mock an open used in a with statement using the Mock framework in Python?
- How do I print to console in pytest?

System Design Fundamentals
Build a strong foundation in designing scalable, reliable distributed systems.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
System Design practice on Codemia
Work through 120+ system design problems with detailed solutions, from rate limiters to multi-region storage.