Mocking Apache HTTPClient using Mockito

后端 未结 5 808
走了就别回头了
走了就别回头了 2020-12-25 11:24

I\'m trying to mock Apache HttpClient Interface in order to mock one of its methods mentioned below to return a stubbed JSON object in response.

HttpResponse         


        
5条回答
  •  無奈伤痛
    2020-12-25 12:05

    In your unit test class you need to mock defaultHttpClient:

    @Mock
    private HttpClient defaultHttpClient;
    

    Then you tell mockito (for example in @Before method) to actually create your mocks by:

    MockitoAnnotations.initMocks(YourTestClass);
    

    Then in your test method you define what execute() method should return:

    when(defaultHttpClient.execute(any()/* or wahtever you want here */)).thenReturn(stubbed JSON object);
    

提交回复
热议问题