I have a class which has direct dependency on the RestTemplate. I wish I have a JUnit test of it, offline.
How could I mock a RestTemplate in my unittest?
Sping 3.0 introduced RestTemplate
. Since version 3.2, the Spring MVC test framework has provided the class MockRestServiceServer
for unit testing client REST code.
I suggest refactoring your client code to remove the direct dependency on RestTemplate
, and replace it with references to RestOperations
, which is the interface implemented by RestTemplate
. and the one you should be coding to.
You can then inject a stub or mock of RestOperations
into your code for unit testing, and inject a RestTemplate
when using it for real.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With