如何使用 Mockito 检查参数是否包含两个子字符串?

发布于 2024-09-28 02:26:19 字数 192 浏览 5 评论 0原文

我的测试中有一行目前看起来像:

Mockito.verify(mockMyObject).myMethod(Mockito.contains("apple"));

我想修改它以检查参数是否同时包含 "apple""banana"。我该怎么办呢?

I have a line in my test that currently looks like:

Mockito.verify(mockMyObject).myMethod(Mockito.contains("apple"));

I would like to modify it to check if the parameter contains both "apple" and "banana". How would I go about this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

吃→可爱长大的 2024-10-05 02:26:19

只需使用 Mockito.matches(String),例如:

Mockito.verify(mockMyObject).
  myMethod(
    Mockito.matches("(.*apple.*banana.*)|(.*banana.*apple.*)"
  )
);

Just use Mockito.matches(String), for example:

Mockito.verify(mockMyObject).
  myMethod(
    Mockito.matches("(.*apple.*banana.*)|(.*banana.*apple.*)"
  )
);
乖乖哒 2024-10-05 02:26:19

从 Java 8 和 Mockito 2.1.0 开始,可以按如下方式使用 Streams:

Mockito.verify(mockMyObject).myMethod(
    Mockito.argThat(s -> s.contains("apple") && s.contains("banana"))
);

从而提高可读性

Since Java 8 and Mockito 2.1.0, it is possible to use Streams as follows:

Mockito.verify(mockMyObject).myMethod(
    Mockito.argThat(s -> s.contains("apple") && s.contains("banana"))
);

thus improving readability

国产ˉ祖宗 2024-10-05 02:26:19

我认为最简单的解决方案是多次调用 verify() :

verify(emailService).sendHtmlMail(anyString(), eq(REPORT_TITLE), contains("Client response31"));
verify(emailService).sendHtmlMail(anyString(), eq(REPORT_TITLE), contains("Client response40"));
verify(emailService, never()).sendHtmlMail(anyString(), anyString(), contains("Client response30"));

I think the easiest solution is to call the verify() multiple times:

verify(emailService).sendHtmlMail(anyString(), eq(REPORT_TITLE), contains("Client response31"));
verify(emailService).sendHtmlMail(anyString(), eq(REPORT_TITLE), contains("Client response40"));
verify(emailService, never()).sendHtmlMail(anyString(), anyString(), contains("Client response30"));
好听的两个字的网名 2024-10-05 02:26:19

也许这不再相关,但我找到了另一种方法来做到这一点,遵循托斯顿的答案和另一个答案。就我而言,我使用 Hamcrest 匹配器

Mockito.verify(mockMyObject).myMethod(
   Mockito.argThat(Matchers.allOf(
      Matchers.containsString("apple"),
      Matchers.containsString("banana"))));

Maybe this is not relevant anymore but I found another way to do it, following Torsten answer and this other answer. In my case I used Hamcrest Matchers

Mockito.verify(mockMyObject).myMethod(
   Mockito.argThat(Matchers.allOf(
      Matchers.containsString("apple"),
      Matchers.containsString("banana"))));
Smile简单爱 2024-10-05 02:26:19

您还可以使用 Mockito 的 AdditionalMatchers

Mockito.verify(mockMyObject).myMethod(
AdditionalMatchers.and(Mockito.contains("apple"), Mockito.contains("banana")));

更多信息 https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/AdditionalMatchers.html#and(T,T )

You can also use Mockito's AdditionalMatchers:

Mockito.verify(mockMyObject).myMethod(
AdditionalMatchers.and(Mockito.contains("apple"), Mockito.contains("banana")));

More info https://www.javadoc.io/doc/org.mockito/mockito-core/latest/org/mockito/AdditionalMatchers.html#and(T,T)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文