Java 使用Mockito时,验证空行失败
尝试使用以下测试测试我的客户端:Java 使用Mockito时,验证空行失败,java,unit-testing,testing,junit,mockito,Java,Unit Testing,Testing,Junit,Mockito,尝试使用以下测试测试我的客户端: private static final HttpPost EXPECTED_POST = new HttpPost(someURI); @Test public void sendingPostRequest() throws IOException { classUnderTest.takeParamsAndSend(REQUEST_STRING); verify(client).execute(EXPECT
private static final HttpPost EXPECTED_POST = new HttpPost(someURI);
@Test
public void sendingPostRequest() throws IOException {
classUnderTest.takeParamsAndSend(REQUEST_STRING);
verify(client).execute(EXPECTED_POST);
verifyNoMoreInteractions(client);
}
在生产代码中是这样的:
URI uri = createURI();
HttpPost post = new HttpPost(uri);
return client.execute(post);
结果是在同一个execute上出现“比较失败”,在实际的execute上出现一个空行。
看起来像这样:
预期:
"client.execute(
POST somePostRequest HTTP/1.1
);"
实际:
"client.execute(
POST somePostRequest HTTP/1.1
);
"
编辑:如注释中所述,大多数Apache HTTP客户端API类不会覆盖
java.lang.Object#equals
,因此您无法可靠地使用org.mockito.ArgumentMatchers#eq(T)
。
您需要使用org.mockito.ArgumentMatchers#argThat
matcher,在谓词中定义相等条件
下面是我如何测试它的:
import static org.mockito.ArgumentMatchers.argThat;
//...
@试验
无效堆栈溢出64222693(){
//给定
HttpClient=mock(HttpClient.class);
URI=URI.create(“https://stackoverflow.com/questions/64222693");
HttpPost=新的HttpPost(uri);
//什么时候
客户。执行(post);
//然后
URI expectedUri=URI.create(“https://stackoverflow.com/questions/64222693");
HttpPost expectedPost=新的HttpPost(expectedUri);
验证(客户端).execute(参数->参数.getURI().equals(expectedPost.getURI())&&
参数.getMethod().equals(expectedPost.getMethod())&&
Arrays.equals(argument.getAllHeaders(),expectedPost.getAllHeaders())&&
参数.getProtocolVersion().equals(expectedPost.getProtocolVersion());
}
编辑:如注释中所述,大多数Apache HTTP客户端API类不会覆盖java.lang.Object#equals
,因此无法可靠地使用org.mockito.ArgumentMatchers#eq(T)
。
您需要使用org.mockito.ArgumentMatchers#argThat
matcher,在谓词中定义相等条件
下面是我如何测试它的:
import static org.mockito.ArgumentMatchers.argThat;
//...
@试验
无效堆栈溢出64222693(){
//给定
HttpClient=mock(HttpClient.class);
URI=URI.create(“https://stackoverflow.com/questions/64222693");
HttpPost=新的HttpPost(uri);
//什么时候
客户。执行(post);
//然后
URI expectedUri=URI.create(“https://stackoverflow.com/questions/64222693");
HttpPost expectedPost=新的HttpPost(expectedUri);
验证(客户端).execute(参数->参数.getURI().equals(expectedPost.getURI())&&
参数.getMethod().equals(expectedPost.getMethod())&&
Arrays.equals(argument.getAllHeaders(),expectedPost.getAllHeaders())&&
参数.getProtocolVersion().equals(expectedPost.getProtocolVersion());
}
现在就尝试了,但在空行上仍然失败您介意给出HttpPost
对象的完整限定名吗?这可能是由于实现不良的Object.equals
方法造成的。您是指org.apache.http.client.methods.HttpPost吗?这就是问题的原因。ApacheHTTP客户端中的大多数对象不会覆盖默认的java.lang.Object#equals
方法,因此导致无法使用org.mockito.ArgumentMatchers#eq(T)
。不幸的是,您需要使用org.mockito.ArgumentMatchers#argThat
。编辑我的答案以向您建议解决方案。现在尝试了,但在空行上仍然失败。您介意提供HttpPost
对象的完整限定名吗?这可能是由于实现不良的Object.equals
方法造成的。您是指org.apache.http.client.methods.HttpPost吗?这就是问题的原因。ApacheHTTP客户端中的大多数对象不会覆盖默认的java.lang.Object#equals
方法,因此导致无法使用org.mockito.ArgumentMatchers#eq(T)
。不幸的是,您需要使用org.mockito.ArgumentMatchers#argThat
。编辑我的答案,为您提供解决方案。