我正在尝试使用 JMockit 测试静态方法。此方法使用实用程序类静态方法。
当测试运行时,JMockit 抱怨并抛出以下异常:
测试集:com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest -------------------------------------------------- ------------------ 测试运行:1,失败:0,错误:1,跳过:0,经过时间:0.086 秒失败! testMakeResourceRequestPositive(com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest) 经过时间:0.085 秒错误! mockit.internal.UnexpectedInvocation:com.company.pkg.subpkg.mylib.NetworkUtil#sendHttpRequest(org.apache.http.client.methods.HttpUriRequest requestToSend) 的参数“requestToSend”预期 GET https://localhost HTTP/1.1,得到获取 https://localhost HTTP/1.1 在 com.company.pkg.subpkg.mylib.NetworkUtil.sendHttpRequest(NetworkUtil.java) 在 com.company.pkg.subpkg.mylib.serviceaccess.client.ServiceCommunicator.makeResourceRequest(ServiceCommunicator.java:57) 在 com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest.testMakeResourceRequestPositive(ServiceCommunicationTest.java:79) 在 sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:606) . . . . . .
这是导致抛出异常的测试 -
@RunWith(JUnit4.class)
public class ServiceCommunicationTest {
@Mocked
private NetworkUtil _mockedNetworkUtil;
@Test
public void testMakeResourceRequestPositive() throws IOException {
ResourcesDTO resDto = null;
final String jsonResponse = JsonResponsesTestUtil.SUCCESSFUL_RESOURCES_RESPONSE;
final String serviceUrl = "https://localhost";
final HttpUriRequest requestToSend = new HttpGet(serviceUrl);
new Expectations() {
{
NetworkUtil.validateStringInput(serviceUrl, "rootUri");
NetworkUtil.sendHttpRequest(requestToSend);
returns(jsonResponse);
}
};
// Make the actual call …
try {
resDto = ServiceCommunicator.makeResourceRequest(serviceUrl);
} catch (IOException e) {
Assert.fail("Failed to parse/obtain the Resources DTO: " + e.getMessage());
}
Assert.assertNotNull(resDto);
Assert.assertNotNull(resDto.getSelfUri());
}
}
一些令人困惑的事情是预期和实际字符串(如异常中报告的)相同 -
预期 GET https://localhost HTTP/1.1 得到了 GET https://localhost HTTP/1.1
被测方法在这里——
public static ResourcesDTO makeResourceRequest(String rootUri) throws IOException {
NetworkUtil.validateStringInput(rootUri, "rootUri");
// Preparing HTTP Request ...
HttpUriRequest requestToSend = new HttpGet(rootUri);
// Headers that needs to be set ...
requestToSend.addHeader(HttpHeaders.ACCEPT, MimeConstants.CONTENT_TYPE_STRING);
// Send Request to Authorization Service ...
String response = NetworkUtil.sendHttpRequest(requestToSend);
// Got the response, construct a DTOs out of it ...
StringReader reader = new StringReader(response);
// Convert the JSON response to appropriate DTO ...
ObjectMapper mapper = new ObjectMapper();
ResourcesDTO resourcesDto = mapper.readValue(reader, ResourcesDTO.class);
return resourcesDto;
}
上述方法引用的实用程序类是这样的 -
public final class NetworkUtil {
// Prevent Instance Creation …
private NetworkUtil() {
}
public static ResourcesDTO makeResourceRequest(String rootUri) throws IOException {
NetworkUtil.validateStringInput(rootUri, "rootUri");
// Preparing HTTP Request ...
HttpUriRequest requestToSend = new HttpGet(rootUri);
// Headers that needs to be set ...
requestToSend.addHeader(HttpHeaders.ACCEPT, MimeConstants.CONTENT_TYPE_STRING);
// Send Request to the Server ...
String response = NetworkUtil.sendHttpRequest(requestToSend);
// Got the response, construct a DTOs out of it ...
StringReader reader = new StringReader(response);
// Convert the JSON response to appropriate DTO ...
ObjectMapper mapper = new ObjectMapper();
ResourcesDTO resourcesDto = mapper.readValue(reader, ResourcesDTO.class);
return resourcesDto;
}
}
谁能看到这里发生了什么?