How to unittest a class using RestTemplate offline?

后端 未结 4 617
南笙
南笙 2020-12-29 20:09

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?

4条回答
  •  孤独总比滥情好
    2020-12-29 21:11

    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.

提交回复
热议问题