我有以下代码根据响应代码重试请求。
public class Sample {
public static HttpClient getInstance() {
HttpClientBuilder builder = HttpClients.custom();
builder.setServiceUnavailableRetryStrategy(new ServiceUnavailableRetryStrategy() {
int waitPeriod = 200;
@Override
public boolean retryRequest(final HttpResponse response, final int executionCount,
final HttpContext context) {
int statusCode = response.getStatusLine().getStatusCode();
return ((statusCode == 429)&& (executionCount < 3));
}
@Override
public long getRetryInterval() {
return waitPeriod;
}
});
return builder.build();
}
}
当我为此方法编写单元测试时,未涵盖getInstance覆盖的方法。(retryRequest, getRetryInterval)我如何编写单元测试来覆盖这些方法。通过谷歌搜索我发现我们可以使用ArgumentCaptor. 我试过下面的代码,但它不起作用。
import static org.mockito.Mockito.when;
import static org.mockito.Mockito.verify;
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.ServiceUnavailableRetryStrategy;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.protocol.HttpContext;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
}
argumentCaptor.capture()总是给我null。我越来越喜欢
org.mockito.exceptions.base.MockitoException:没有捕获到参数值!您可能忘记在 verify() 中使用 argument.capture()... ...或者您在存根中使用了 capture() 但未调用存根方法。谁可以帮我这个事。我想测试 retryRequest 方法的功能。
繁花不似锦
相关分类