java - 使用 Mockito 模拟 HttpURLConnection 的问题

标签 java junit mockito httpurlconnection

我正在尝试模拟 HttpURLConnection 对象,但我似乎无法正确处理。 这是我想测试的方法。

@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());
}

这是我所做的。

 @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);
}

这就是我的设置方法。此方法调用的方法的测试用例是成功的。我的实际测试用例如下。

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

我尝试打印数据,它显示为空。

最佳答案

目前您仅测试模拟。在模拟上调用 httpConnectGithubHandle.connect() 并且模拟返回 null,因为没有定义行为。您应该在测试中使用真实的 HttpConnectGithub 对象。 (取消注释测试的第一行并删除 HttpConnectGithub 模拟。)

关于java - 使用 Mockito 模拟 HttpURLConnection 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19599934/

相关文章:

java应用程序与java servlet jdbc连接

java - 用于纯 Java 的 Dropbox Sync API 或 Core API

java - 如何在 C++ 中编写具有多个数据字段的类 Java 枚举类?

java - 使用Junit的三角测试: void type is not allowed

unit-testing - JBoss Arquillian 与 Apache Ant 和 Junit 的集成

java - 使用 Mockito 和 CDI 时如何不模拟对象?

java - 如何在双向关系中删除一侧的对象?

java - 删除或注释掉不工作的 JUnit 测试?

java.lang.AssertionError : Status expected:<200> but was:<404>

java - 使用 PowerMock 模拟私有(private)方法,但仍会调用底层方法