英文:
When using Mockito verify failing on empty row
问题
尝试使用以下测试代码来测试我的客户端:
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
方法上出现了“Comparison Failure”,而且在实际输出中,有一行空行。大致如下所示:
期望输出:
"client.execute(
POST somePostRequest HTTP/1.1
);"
实际输出:
"client.execute(
POST somePostRequest HTTP/1.1
);
"
<details>
<summary>英文:</summary>
Tried to test my client with this following test:
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);
}
And in the production code it's something like this:
URI uri = createURI();
HttpPost post = new HttpPost(uri);
return client.execute(post);
The result is a "Comparison Failure" on the same execute and on the actual, there is an empty row.
Looks something like this:
expected:
"client.execute(
POST somePostRequest HTTP/1.1
);"
actual:
"client.execute(
POST somePostRequest HTTP/1.1
);
"
</details>
# 答案1
**得分**: 0
edit: 如评论中所述,大多数 Apache HTTP 客户端 API 类并没有覆盖 `java.lang.Object#equals` 方法,因此您不能可靠地使用 `org.mockito.ArgumentMatchers#eq(T)` 方法。
您应该使用 `org.mockito.ArgumentMatchers#argThat` 匹配器,在谓词中定义您的相等条件。
以下是我如何测试它:
```java
import static org.mockito.ArgumentMatchers.argThat;
//...
@Test
void stackOverflow64222693() {
// Given
HttpClient client = mock(HttpClient.class);
URI uri = URI.create("https://stackoverflow.com/questions/64222693");
HttpPost post = new HttpPost(uri);
// When
client.execute(post);
// Then
URI expectedUri = URI.create("https://stackoverflow.com/questions/64222693");
HttpPost expectedPost = new HttpPost(expectedUri);
verify(client).execute(argThat(argument -> argument.getURI().equals(expectedPost.getURI()) &&
argument.getMethod().equals(expectedPost.getMethod()) &&
Arrays.equals(argument.getAllHeaders(), expectedPost.getAllHeaders()) &&
argument.getProtocolVersion().equals(expectedPost.getProtocolVersion())));
}
英文:
edit: as stated in comments, most Apache HTTP client API classes does not override java.lang.Object#equals
, hence you cannot reliably use org.mockito.ArgumentMatchers#eq(T)
.
You will want to use org.mockito.ArgumentMatchers#argThat
matcher, defining your equality condition in the predicate.
Here is how I tested it:
import static org.mockito.ArgumentMatchers.argThat;
//...
@Test
void stackOverflow64222693() {
// Given
HttpClient client = mock(HttpClient.class);
URI uri = URI.create("https://stackoverflow.com/questions/64222693");
HttpPost post = new HttpPost(uri);
// When
client.execute(post);
// Then
URI expectedUri = URI.create("https://stackoverflow.com/questions/64222693");
HttpPost expectedPost = new HttpPost(expectedUri);
verify(client).execute(argThat(argument -> argument.getURI().equals(expectedPost.getURI()) &&
argument.getMethod().equals(expectedPost.getMethod()) &&
Arrays.equals(argument.getAllHeaders(), expectedPost.getAllHeaders()) &&
argument.getProtocolVersion().equals(expectedPost.getProtocolVersion())));
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论