模拟HttpClient.execute问题:Mockito

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

我正在尝试测试此方法。

I am trying to test this method.

@Override public JSON connectResource() throws IOException { //get the location and credentials for the certificates System.setProperty("javax.ssl.trustStore", "C:/Program Files/Java/jdk1.7.0_40/jre/lib/security/cacerts"); System.setProperty("javax.ssl.trustStorePassword", "changeit"); HttpRequest httpRequest = new HttpGet(url); System.out.println("hello"); httpRequest.addHeader("Accept", "application/json"); HttpResponse response = httpClient.execute((HttpUriRequest) httpRequest); System.out.println("hello1"); HttpEntity httpEntity = response.getEntity(); String data = this.getData(httpEntity); return JSONSerializer.toJSON(data.toString()); }

我的设置方法是:

@Before public void setUp() throws Exception{ mockHttpClient = mock(DefaultHttpClient.class); mockHttpRequest = mock(HttpUriRequest.class); mockHttpResponse = mock(BasicHttpResponse.class); mockHttpEntity = mock(HttpEntity.class); mockInputStream = mock(InputStream.class); mockInputStreamReader = mock(InputStreamReader.class); mockBufferedReader = mock(BufferedReader.class); mockHttpGet = mock(HttpGet.class); mockHttpRequestBase = mock(HttpRequestBase.class); //when(mockHttpClient.execute(Mockito.isA(HttpUriRequest.class))).thenReturn(mockHttpResponse); //when(mockHttpClient.execute(mockHttpRequest)).thenReturn(mockHttpResponse); //when(mockHttpClient.execute(mockHttpRequestBase)).thenReturn(mockHttpResponse); //when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse); when(mockHttpResponse.getEntity()).thenReturn(mockHttpEntity); when(mockHttpEntity.getContent()).thenReturn(mockInputStream); PowerMockito.whenNew(InputStreamReader.class) .withArguments(mockInputStream).thenReturn(mockInputStreamReader); PowerMockito.whenNew(BufferedReader.class) .withArguments(mockInputStreamReader).thenReturn(mockBufferedReader); PowerMockito.when(mockBufferedReader.readLine()) .thenReturn(JSON_STRING) .thenReturn(null); PowerMockito.whenNew(HttpGet.class).withArguments(VALID_URL) .thenReturn(mockHttpGet); }

我的测试用例是:

@Test public void testConnectResource() throws IOException { when(mockHttpClient.execute(mockHttpGet)).thenReturn(mockHttpResponse); HttpConnectGithub connHandle = new HttpConnectGithub(VALID_URL); JSON jsonObject = connHandle.connectResource(); System.out.println(jsonObject); //assertThat(jsonObject, instanceOf(JSON.class)); }

然而,执行在

HttpResponse response = httpClient.execute((HttpUriRequest) httpRequest);

你可以在我的设置方法的评论中看到我尝试的所有内容。 有没有人发现任何问题?我通过我的测试用例进行了调试,并且所有模拟对象都已正确初始化。 我尝试过交换HttpUriRequest和HttpRequest,HttpResponse和BasicHttpResponse等但没有太多运气。 请指导如何解决这个问题。

you can see all that I tried in the comments of my set up method. Does anyone find an issue with anything? I debugged through my test case and all mock objects are properly initialized. I have tried exchanging HttpUriRequest and HttpRequest, HttpResponse and BasicHttpResponse etc but without much luck. Please guide on how to tackle this issue.

推荐答案

问题出在mockHttpClient上。由于某种原因,它无法自动模拟它。修复是通过某种方法(在我的例子中是构造函数)将httpclient作为参数传递

The problem is with mockHttpClient. It is not able to mock it automatically for some reason. The fix is to pass httpclient as a parameter through some method (constructor in my case)

更多推荐

模拟HttpClient.execute问题:Mockito

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

发布评论

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

>www.elefans.com

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