使用Mockito模拟Apache HTTPClient

编程入门 行业动态 更新时间:2024-10-23 15:26:01
本文介绍了使用Mockito模拟Apache HTTPClient的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在尝试模拟Apache HttpClient接口,以便模拟下面提到的一种方法来返回一个存根的JSON对象作为响应。

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 response = defaultHttpClient.execute(postRequest);

有人可以通过一些示例代码建议如何实现这一目标吗?非常感谢您的帮助。

Could somebody be able to suggest how to achieve this with some sample code? Your help would be greatly appreciated.

谢谢

推荐答案

以下是我测试代码所做的工作使用Mockito和Apache HttpBuilder:

Here is what I did to test my code using Mockito and Apache HttpBuilder:

受测试的类别:

import java.io.BufferedReader; import java.io.IOException; import javax.ws.rs.core.Response.Status; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.impl.client.HttpClientBuilder; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class StatusApiClient { private static final Logger LOG = LoggerFactory.getLogger(StatusApiClient.class); private String targetUrl = ""; private HttpClient client = null; HttpGet httpGet = null; public StatusApiClient(HttpClient client, HttpGet httpGet) { this.client = client; this.httpGet = httpGet; } public StatusApiClient(String targetUrl) { this.targetUrl = targetUrl; this.client = HttpClientBuilder.create().build(); this.httpGet = new HttpGet(targetUrl); } public boolean getStatus() { BufferedReader rd = null; boolean status = false; try{ LOG.debug("Requesting status: " + targetUrl); HttpResponse response = client.execute(httpGet); if(response.getStatusLine().getStatusCode() == Status.OK.getStatusCode()) { LOG.debug("Is online."); status = true; } } catch(Exception e) { LOG.error("Error getting the status", e); } finally { if (rd != null) { try{ rd.close(); } catch (IOException ioe) { LOG.error("Error while closing the Buffered Reader used for reading the status", ioe); } } } return status; } }

测试:

import java.io.IOException; import org.apache.http.HttpResponse; import org.apache.http.StatusLine; import org.apache.http.client.ClientProtocolException; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpGet; import org.apache.http.conn.HttpHostConnectException; import org.junit.Assert; import org.junit.Test; import org.mockito.Mockito; public class StatusApiClientTest extends Mockito { @Test public void should_return_true_if_the_status_api_works_properly() throws ClientProtocolException, IOException { //given: HttpClient httpClient = mock(HttpClient.class); HttpGet httpGet = mock(HttpGet.class); HttpResponse httpResponse = mock(HttpResponse.class); StatusLine statusLine = mock(StatusLine.class); //and: when(statusLine.getStatusCode()).thenReturn(200); when(httpResponse.getStatusLine()).thenReturn(statusLine); when(httpClient.execute(httpGet)).thenReturn(httpResponse); //and: StatusApiClient client = new StatusApiClient(httpClient, httpGet); //when: boolean status = client.getStatus(); //then: Assert.assertTrue(status); } @Test public void should_return_false_if_status_api_do_not_respond() throws ClientProtocolException, IOException { //given: HttpClient httpClient = mock(HttpClient.class); HttpGet httpGet = mock(HttpGet.class); HttpResponse httpResponse = mock(HttpResponse.class); StatusLine statusLine = mock(StatusLine.class); //and: when(httpClient.execute(httpGet)).thenThrow(HttpHostConnectException.class); //and: StatusApiClient client = new StatusApiClient(httpClient, httpGet); //when: boolean status = client.getStatus(); //then: Assert.assertFalse(status); } }

你觉得大家好吗?我需要改进一些东西吗? (是的,我知道,评论。这是我从我的Spock背景中得到的:D)

What do you think folks, do I need to improve something? (Yeah, I know, the comments. That is something I brought from my Spock background :D)

更多推荐

使用Mockito模拟Apache HTTPClient

本文发布于:2023-10-31 07:41:17,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1545464.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Mockito   Apache   HTTPClient

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!