我正在尝试使用JMockit测试静态方法。此方法使用实用程序类静态方法。
当测试运行时,JMockit会抱怨并抛出以下异常:
Test set: com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest -------------------------------------------------------------------- Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.086 sec FAILURE! testMakeResourceRequestPositive(com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest) Time elapsed: 0.085 sec ERROR! mockit.internal.UnexpectedInvocation: Parameter "requestToSend" of com.company.pkg.subpkg.mylib.NetworkUtil#sendHttpRequest(org.apache.http.client.methods.HttpUriRequest requestToSend) expected GET https://localhost HTTP/1.1, got GET https://localhost HTTP/1.1 at com.company.pkg.subpkg.mylib.NetworkUtil.sendHttpRequest(NetworkUtil.java) at com.company.pkg.subpkg.mylib.serviceaccess.client.ServiceCommunicator.makeResourceRequest(ServiceCommunicator.java:57) at com.company.pkg.subpkg.mylib.test.ServiceCommunicationTest.testMakeResourceRequestPositive(ServiceCommunicationTest.java:79) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at 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());
}
}
令人困惑的一点是,预期字符串和实际字符串(如异常中所报告的那样) -
expected GET https://localhost HTTP/1.1 got 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;
}
}
谁能看到这里发生了什么?
答案 0 :(得分:4)
最后,我能够确定这种意外调用异常的原因。
NetworkUtil.sendHttpRequest((HttpUriRequest) any);
returns(jsonResponse);
如上所示,我在Expectations块中更改了返回之前的行。现在我告诉JMockit 任何类型为HttpUriRequest的对象都是可接受的,而不是传递HttpUriRequest类型的对象。
我相信,JMockit对模拟API调用的参数执行equals()。这里的问题可能是HttpUriRequest deosn实现了自己的equals()方法,而不是从Object继承。这意味着它实际上是在做一个object1 == object2。