java - 使用 Mockito 和 jUnit 为具有两个 HTTP 调用的方法编写单元测试

标签 java unit-testing junit mocking mockito

我有三种方法,

  • void save(字符串 a, 字符串 b)
  • String getId(String a, Pojo p)
  • void update(String a, String b, StringEntity c)

这是实现(不是完整的实现),

String getId(String accessToken, Pojo p) {

    //Note that the pojo is for formatting the payload for the request, nothing more

    HttpResponse response = httpRequestService.makeGetRequest(url, TOKEN_PREFIX, accessToken,
            CONTENT_TYPE);
    if (response.getStatusLine().getStatusCode() == 200) {
        log.debug("Success");
    }

    //Code for getting the id from the response

    return id;
}

void update(String accassToken, String id, StringEntity payload) {

    HttpResponse response = httpRequestService.makePutRequest(url + id,
            TOKEN_PREFIX, accessToken, CONTENT_TYPE, payload);

    if (response.getStatusLine().getStatusCode() == 200) {
        log.debug("Success");
    }
}

void save(String accessToken, String payload) {

    //The getId() is called here
    String id = getId(/*arguments*/);

    if(id == null) {
        log.error("Error");
    } else {
        //The update() is called here
        update(/*arguments*/);
    }
}

如上所述,getId 和 update 方法在 save 方法内部调用,并且 getId 和 update 方法都有 HTTP 调用。

我必须为 save() 方法编写一个单元测试。这是我尝试过的。

    //This is an interface with methods to call HTTP requests.
    HttpRequestService httpRequestService = Mockito.mock(HttpRequestService.class);

    //Constructor injection
    ClassWithMethods a = new ClasswithMethods(httpRequestService);

    HttpResponse responseGet = Mockito.mock(HttpResponse.class);
    StatusLine statusLineGet = Mockito.mock(StatusLine.class);
    HttpEntity httpEntity = Mockito.mock(HttpEntity.class);
    Mockito.when(responseGet.getStatusLine()).thenReturn(statusLineGet);
    Mockito.when(statusLineGet.getStatusCode()).thenReturn(200);
    Mockito.when(responseGet.getEntity()).thenReturn(httpEntity);
    Mockito.when(httpEntity.getContent()).thenReturn(IOUtils.toInputStream(stream));
    Mockito.when(httpRequestService.makeGetRequest(url, TOKEN_PREFIX, accessToken,
            CONTENT_TYPE).thenReturn(responseGet);

    HttpResponse responsePut = Mockito.mock(HttpResponse.class);
    StatusLine statusLinePut = Mockito.mock(StatusLine.class);
    Mockito.when(responsePut.getStatusLine()).thenReturn(statusLinePut);
    Mockito.when(statusLinePut.getStatusCode()).thenReturn(200);
    Mockito.when(httpRequestService.makePutRequest(url + id, TOKEN_PREFIX,
            accessToken, CONTENT_TYPE, payloadEntity).thenReturn(responsePut);

    a.save(accessToken, payload);

但是在测试时,responsePut 返回 null。论据也相匹配。

问题:如何测试这个调用两个HTTP方法的保存方法?

这可能不是最好的方法。如果有更好的方法来测试这样的方法,请提出建议。

谢谢

请注意,httpRequestService 是一个带有 HTTP 调用方法的接口(interface),并且也使用了构造函数注入(inject)。

最佳答案

您应该将以下内容提取到一个单独的方法中(PUT 也是如此):

HttpResponse response = httpRequestService.makeGetRequest(url, TOKEN_PREFIX, accessToken,
            CONTENT_TYPE);

然后你可以使用mockito来 stub 该方法:

when(obj.methodGET(...)).thenReturns(...)

记住:您的单元测试不应该通过网络进行调用!

编辑

private HttpResponse get(String url, ...) {
    return httpRequestService.makeGetRequest(url, TOKEN_PREFIX, accessToken,
            CONTENT_TYPE);
}


private HttpResponse put(String url, ...) {
    return httpRequestService.makePutRequest(url + id,
        TOKEN_PREFIX, accessToken, CONTENT_TYPE, payload);
}

然后:

...
when(mockedStatusLineObj.getStatusCode()).thenReturns(200);
HttpResponse mockedGet = mock(HttpResponse.class);
when(mockedGet.getStatusLine()).thenReturns(mockedStatusLineObj);
when(object.get(...)).thenReturns(mockedGet);
when(object.put(...)).thenReturns(...);

关于java - 使用 Mockito 和 jUnit 为具有两个 HTTP 调用的方法编写单元测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47178048/

相关文章:

java - 列出所有可能的所有者和组

java - 使用 ascii 码检查空格

c# - 在 nunit 测试中使用连接字符串

java - Oracle UCM RIDC 服务使用 GET_SEARCH_RESULTS 获取文件夹路径

Java - 多线程CPU缓存

c# - 如何重新安排单元测试?

javascript - 停止 Jest 对 TS 文件运行测试,并且仅对 JS 文件运行测试

java - 使用 JUnit 测试 ServiceLocator

unit-testing - 测试 Controller 是否有意义

spring - 在 Maven 布局中使用 Spring 或 Camel 属性占位符覆盖 junit 测试的一些属性