Testing methods that make http requests

前端 未结 7 1328
悲哀的现实
悲哀的现实 2021-02-09 01:31

I have some methods in my app that make http requests. Is there a good way to simulate network failures and bad responses for the unit tests?

7条回答
  •  生来不讨喜
    2021-02-09 02:26

    Wrap the library that makes the HTTP calls (e.g. java.net.URLConnection or Commons HttpClient) behind an interface, and then write implementations or mocks of that interface that simulates the failure conditions.

    Your interface can just represent the operations that your application needs to perform, rather than the whole range of HTTP client functionality.

提交回复
热议问题