Mocked HttpClient calls actual method

情到浓时终转凉″ 提交于 2019-12-02 09:12:30

From the code snippet shared it is evident that HttpClient and HttpPost are instantiated via new operator instead of using any instance level injected (i.e. autowired) objects.

 final HttpClient httpClient = new DefaultHttpClient();
 final HttpPost postRequest = new HttpPost(someURL);

Thus the @InjectMocks and @Mock essentially have no effect and the real instance of HttpClient and HttpPost are used when invoked from test class; which explains the error encountered.

In this particular scenario Mockito (perhaps any mocking framework) can't help. To proceed, if possible, refactor the code under test such as to use the instance level injected object(s) instead of using the instance created via new operator.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!