gpt4 book ai didi

java - 模拟 CloseableHttpClient 在测试时仍将连接传递到真实服务器

转载 作者:行者123 更新时间:2023-12-02 12:57:12 24 4
gpt4 key购买 nike

我尝试测试一种使用 CloseableHttpClient 连接的方法。我决定选择 Mockito 并模拟所有相关的类和方法。但是,一旦我开始测试,它就会直接进入真实服务器,而不是被模拟的 CloseableHttpClient 拦截。

测试

  @Test
public void testBid() throws IOException {
//given:
HttpGet httpGet = mock(HttpGet.class);
HttpResponse httpResponse = mock(HttpResponse.class);

StatusLine statusLine = mock(StatusLine.class);

ObserverImp observer = mock(ObserverImp.class);

CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);

//and:
when(statusLine.getStatusCode()).thenReturn(200);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(closeableHttpClient.execute(httpGet)).thenReturn(closeableHttpResponse);
when(observer.getKey()).thenReturn("##213");


Buy buy = new Buy(observer);
buy.bid(14455);
}

以及相关的实现

public void buy(double price) {
String queryArgs = "command=order&amount=1" + "&price=" + String.valueOf(price); // generates query

CloseableHttpClient httpClient = HttpClients.createDefault();
HttpPost post = new HttpPost("wwww.hm.edu/projectGroup1");
post.addHeader("Key", observer.getKey());
try {
post.setEntity(new ByteArrayEntity(queryArgs.getBytes("UTF-8")));
} catch (UnsupportedEncodingException e) {
System.out.println("Exception in run");
}
List<NameValuePair> params = new ArrayList<>();

params.add(new BasicNameValuePair("command", "order"));
params.add(new BasicNameValuePair("amount", "1"));
params.add(new BasicNameValuePair("price", String.valueOf(price)));
try {
post.setEntity(new UrlEncodedFormEntity(params));
CloseableHttpResponse response = httpClient.execute(post);
HttpEntity entity = response.getEntity();
Scanner in = new Scanner(entity.getContent());
String orderNumber = "";
while (in.hasNext()) {
orderNumber = in.nextLine();
}
String[] findOrderNumber = orderNumber.split(".");
long lastOrderNumber = -1;
try {
lastOrderNumber = Long.valueOf(findOrderNumber[3]);
} catch (NumberFormatException exception) {
System.out.println("NumberFormatException");
} finally {
if (lastOrderNumber != -1) {
observer.setOrderNumber(lastOrderNumber);
}
}
in.close();
EntityUtils.consume(entity);
httpClient.close();
} catch (IOException e) {
System.out.println("Exception occured during process");
}
}

我尝试了在教程或类似问题中找到的几个测试代码片段,但没有成功。你能告诉我我在这里做错了什么吗?

谢谢:)

最佳答案

您可以通过构造函数注入(inject)CloseableHttpClient httpClient:

public class Buy {
private ObserverImp observer;
private CloseableHttpClient httpClient;

public Buy (ObserverImp observer, CloseableHttpClient httpClient) {
this.observer = observer;
this.httpClient = httpClient;
}

public void buy(double price) {
...

// Comment or remove the line below and use the injected instead...
// CloseableHttpClient httpClient = HttpClients.createDefault();

...
}
}

要测试它,请执行以下操作:

@Test
public void testBid() throws IOException {
//given:
HttpGet httpGet = mock(HttpGet.class);
HttpResponse httpResponse = mock(HttpResponse.class);

StatusLine statusLine = mock(StatusLine.class);

ObserverImp observer = mock(ObserverImp.class);

CloseableHttpClient closeableHttpClient = mock(CloseableHttpClient.class);
CloseableHttpResponse closeableHttpResponse = mock(CloseableHttpResponse.class);

//and:
when(statusLine.getStatusCode()).thenReturn(200);
when(httpResponse.getStatusLine()).thenReturn(statusLine);
when(closeableHttpClient.execute(httpGet)).thenReturn(closeableHttpResponse);
when(observer.getKey()).thenReturn("##213");


Buy buy = new Buy(observer, closeableHttpClient);
buy.bid(14455);
}

关于java - 模拟 CloseableHttpClient 在测试时仍将连接传递到真实服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44396295/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com