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

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

When using Mockito verify failing on empty row

问题

尝试使用以下测试代码来测试我的客户端:

  1. private static final HttpPost EXPECTED_POST = new HttpPost(someURI);
  2. @Test
  3. public void sendingPostRequest() throws IOException {
  4. classUnderTest.takeParamsAndSend(REQUEST_STRING);
  5. verify(client).execute(EXPECTED_POST);
  6. verifyNoMoreInteractions(client);
  7. }

在生产代码中,大致是这样的:

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

结果是在相同的execute方法上出现了“Comparison Failure”,而且在实际输出中,有一行空行。大致如下所示:

期望输出:

  1. "client.execute(
  2. POST somePostRequest HTTP/1.1
  3. );"

实际输出:

  1. "client.execute(
  2. POST somePostRequest HTTP/1.1
  3. );
  4. "
  5. <details>
  6. <summary>英文:</summary>
  7. Tried to test my client with this following test:
  8. private static final HttpPost EXPECTED_POST = new HttpPost(someURI);
  9. @Test
  10. public void sendingPostRequest() throws IOException {
  11. classUnderTest.takeParamsAndSend(REQUEST_STRING);
  12. verify(client).execute(EXPECTED_POST);
  13. verifyNoMoreInteractions(client);
  14. }
  15. And in the production code it&#39;s something like this:
  16. URI uri = createURI();
  17. HttpPost post = new HttpPost(uri);
  18. return client.execute(post);
  19. The result is a &quot;Comparison Failure&quot; on the same execute and on the actual, there is an empty row.
  20. Looks something like this:
  21. expected:
  22. &quot;client.execute(
  23. POST somePostRequest HTTP/1.1
  24. );&quot;
  25. actual:
  26. &quot;client.execute(
  27. POST somePostRequest HTTP/1.1
  28. );
  29. &quot;
  30. </details>
  31. # 答案1
  32. **得分**: 0
  33. edit: 如评论中所述,大多数 Apache HTTP 客户端 API 类并没有覆盖 `java.lang.Object#equals` 方法,因此您不能可靠地使用 `org.mockito.ArgumentMatchers#eq(T)` 方法。
  34. 您应该使用 `org.mockito.ArgumentMatchers#argThat` 匹配器,在谓词中定义您的相等条件。
  35. 以下是我如何测试它:
  36. ```java
  37. import static org.mockito.ArgumentMatchers.argThat;
  38. //...
  39. @Test
  40. void stackOverflow64222693() {
  41. // Given
  42. HttpClient client = mock(HttpClient.class);
  43. URI uri = URI.create("https://stackoverflow.com/questions/64222693");
  44. HttpPost post = new HttpPost(uri);
  45. // When
  46. client.execute(post);
  47. // Then
  48. URI expectedUri = URI.create("https://stackoverflow.com/questions/64222693");
  49. HttpPost expectedPost = new HttpPost(expectedUri);
  50. verify(client).execute(argThat(argument -> argument.getURI().equals(expectedPost.getURI()) &&
  51. argument.getMethod().equals(expectedPost.getMethod()) &&
  52. Arrays.equals(argument.getAllHeaders(), expectedPost.getAllHeaders()) &&
  53. argument.getProtocolVersion().equals(expectedPost.getProtocolVersion())));
  54. }
英文:

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:

  1. import static org.mockito.ArgumentMatchers.argThat;
  2. //...
  3. @Test
  4. void stackOverflow64222693() {
  5. // Given
  6. HttpClient client = mock(HttpClient.class);
  7. URI uri = URI.create(&quot;https://stackoverflow.com/questions/64222693&quot;);
  8. HttpPost post = new HttpPost(uri);
  9. // When
  10. client.execute(post);
  11. // Then
  12. URI expectedUri = URI.create(&quot;https://stackoverflow.com/questions/64222693&quot;);
  13. HttpPost expectedPost = new HttpPost(expectedUri);
  14. verify(client).execute(argThat(argument -&gt; argument.getURI().equals(expectedPost.getURI()) &amp;&amp;
  15. argument.getMethod().equals(expectedPost.getMethod()) &amp;&amp;
  16. Arrays.equals(argument.getAllHeaders(), expectedPost.getAllHeaders()) &amp;&amp;
  17. argument.getProtocolVersion().equals(expectedPost.getProtocolVersion())));
  18. }

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:

确定