使用Mockito进行验证时失败,出现空行问题。

huangapple go评论77阅读模式
英文:

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&#39;s something like this:

    URI uri = createURI();
    HttpPost post = new HttpPost(uri);
    return client.execute(post);

The result is a &quot;Comparison Failure&quot; on the same execute and on the actual, there is an empty row.
Looks something like this:
expected:

    &quot;client.execute(
        POST somePostRequest HTTP/1.1
    );&quot;

actual:

    &quot;client.execute(
       POST somePostRequest HTTP/1.1
    );
    &quot;

</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(&quot;https://stackoverflow.com/questions/64222693&quot;);
    HttpPost   post   = new HttpPost(uri);

    // When
    client.execute(post);

    // Then
    URI      expectedUri  = URI.create(&quot;https://stackoverflow.com/questions/64222693&quot;);
    HttpPost expectedPost = new HttpPost(expectedUri);

    verify(client).execute(argThat(argument -&gt; argument.getURI().equals(expectedPost.getURI()) &amp;&amp;
                                               argument.getMethod().equals(expectedPost.getMethod()) &amp;&amp;
                                               Arrays.equals(argument.getAllHeaders(), expectedPost.getAllHeaders()) &amp;&amp;
                                               argument.getProtocolVersion().equals(expectedPost.getProtocolVersion())));
  }

huangapple
  • 本文由 发表于 2020年10月6日 17:09:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64222693.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定