简体   繁体   中英

Issues with Mocking HttpURLConnection using Mockito

I am trying to mock a HttpURLConnection object but I cant seem to get it right. Here is the method I would like to test.

@Override
public JSON connect() throws IOException {
    HttpURLConnection httpConnection;
    String finalUrl = url;
    URL urlObject = null;
    int status = 0;
    //recursively check for redirected uri if the given uri is moved
    do{
            urlObject = getURL(finalUrl);
            httpConnection = (HttpURLConnection) urlObject.openConnection();
            //httpConnection.setInstanceFollowRedirects(true);
            //httpConnection.connect();
            status = httpConnection.getResponseCode();
            if (300 > status && 400 < status){
                continue;
            }
            String redirectedUrl =    httpConnection.getHeaderField("Location");
            if(null == redirectedUrl){
                    break;
            }
            finalUrl =redirectedUrl;

    }while (httpConnection.getResponseCode() != HttpURLConnection.HTTP_OK);
    return  JSONSerializer.toJSON(getData(httpConnection).toString());
}

Here is what I have done.

 @Before
public void setUp() throws Exception{
    //httpConnectGithubHandle = new HttpConnectGithub(VALID_URL);
    httpConnectGithubHandle = mock(HttpConnectGithub.class);
    testURL               = new URL(VALID_URL);
    mockHttpURLConnection = mock(HttpURLConnection.class);  
    mockInputStreamReader = mock(InputStreamReader.class);
    mockBufferedReader    = mock(BufferedReader.class);
    mockInputStream       = mock(InputStream.class);
    when(httpConnectGithubHandle.getData(mockHttpURLConnection)).thenReturn(SOME_STRING);
    when(httpConnectGithubHandle.getURL(SOME_STRING)).thenReturn(testURL);
    when(mockHttpURLConnection.getResponseCode()).thenReturn(200);
    when(mockHttpURLConnection.getHeaderField(LOCATION)).thenReturn(SOME_STRING);
    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);
}

That was my setUp method. The test cases for methods that are called by this method are successful. And my actual test case is as follows.

 @Test
    public void testConnect() throws IOException {
        JSON jsonObject = httpConnectGithubHandle.connect();
        System.out.println(jsonObject);
        assertThat(jsonObject, instanceOf(JSON.class));
    }

I tried to print data, it shows null.

Currently your're only testing the mock. httpConnectGithubHandle.connect() is called on the mock and the mock returns null, because no behaviour is defined. You should use a real HttpConnectGithub object in your test. (Uncomment the first line of your test and remove the HttpConnectGithub mock.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM