1. <small id='PEulG'></small><noframes id='PEulG'>

          <bdo id='PEulG'></bdo><ul id='PEulG'></ul>
        <tfoot id='PEulG'></tfoot>

        <legend id='PEulG'><style id='PEulG'><dir id='PEulG'><q id='PEulG'></q></dir></style></legend>
        <i id='PEulG'><tr id='PEulG'><dt id='PEulG'><q id='PEulG'><span id='PEulG'><b id='PEulG'><form id='PEulG'><ins id='PEulG'></ins><ul id='PEulG'></ul><sub id='PEulG'></sub></form><legend id='PEulG'></legend><bdo id='PEulG'><pre id='PEulG'><center id='PEulG'></center></pre></bdo></b><th id='PEulG'></th></span></q></dt></tr></i><div id='PEulG'><tfoot id='PEulG'></tfoot><dl id='PEulG'><fieldset id='PEulG'></fieldset></dl></div>
      2. mockito 验证与 ArgumentCaptor 的交互

        时间:2023-10-01
        <legend id='KQ1Ks'><style id='KQ1Ks'><dir id='KQ1Ks'><q id='KQ1Ks'></q></dir></style></legend>

      3. <small id='KQ1Ks'></small><noframes id='KQ1Ks'>

            <tbody id='KQ1Ks'></tbody>
              • <bdo id='KQ1Ks'></bdo><ul id='KQ1Ks'></ul>

                  <tfoot id='KQ1Ks'></tfoot>
                • <i id='KQ1Ks'><tr id='KQ1Ks'><dt id='KQ1Ks'><q id='KQ1Ks'><span id='KQ1Ks'><b id='KQ1Ks'><form id='KQ1Ks'><ins id='KQ1Ks'></ins><ul id='KQ1Ks'></ul><sub id='KQ1Ks'></sub></form><legend id='KQ1Ks'></legend><bdo id='KQ1Ks'><pre id='KQ1Ks'><center id='KQ1Ks'></center></pre></bdo></b><th id='KQ1Ks'></th></span></q></dt></tr></i><div id='KQ1Ks'><tfoot id='KQ1Ks'></tfoot><dl id='KQ1Ks'><fieldset id='KQ1Ks'></fieldset></dl></div>
                  本文介绍了mockito 验证与 ArgumentCaptor 的交互的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  要检查与方法调用中的参数属于某种类型的模拟的交互次数,可以这样做

                  To check the number of interactions with a mock where the parameter in the method call is of a certain type, one can do

                  mock.someMethod(new FirstClass());
                  mock.someMethod(new OtherClass());
                  verify(mock, times(1)).someMethod(isA(FirstClass.class));
                  

                  这要归功于对 isA 的调用,因为 someMethod 被调用了两次,但只有一次使用参数 FirstClass

                  This will pass thanks to the call to isA since someMethod was called twice but only once with argument FirstClass

                  然而,当使用 ArgumentCaptor 时,这种模式似乎是不可能的,即使 Captor 是为特定参数 FirstClass

                  However, this pattern seems to not be possible when using an ArgumentCaptor, even if the Captor was created for the particular argument FirstClass

                  这不起作用

                  mock.someMethod(new FirstClass());
                  mock.someMethod(new OtherClass());
                  ArgumentCaptor<FirstClass> captor = ArgumentCaptor.forClass(FirstClass.class);
                  verify(mock, times(1)).someMethod(captor.capture());
                  

                  它说模拟被多次调用.

                  在捕获参数以供进一步检查时,有什么方法可以完成此验证?

                  Is there any way to accomplish this verification while capturing the argument for further checking?

                  推荐答案

                  我建议使用 Mockito 的 Hamcrest 集成为其编写一个好的、干净的匹配器.这允许您将验证与传递参数的详细检查结合起来:

                  I recommend using Mockito's Hamcrest integration to write a good, clean matcher for it. That allows you to combine the verification with detailed checking of the passed argument:

                  verify(mock, times(1)).someMethod(argThat(personNamed("Bob")));
                  
                  Matcher<Person> personNamed(final String name) {
                      return new TypeSafeMatcher<Person>() {
                          public boolean matchesSafely(Person item) {
                              return name.equals(item.getName());
                          }
                          public void describeTo(Description description) {
                              description.appendText("a Person named " + name);
                          }
                      };
                  }
                  

                  匹配器通常会导致更易读的测试和更有用的测试失败消息.它们也往往是非常可重用的,您会发现自己建立了一个为测试您的项目量身定制的库.最后,您还可以使用 JUnit 的 Assert.assertThat() 将它们用于正常的测试断言,因此您可以双重使用它们.

                  Matchers generally lead to more readable tests and more useful test failure messages. They also tend to be very reusable, and you'll find yourself building up a library of them tailored for testing your project. Finally, you can also use them for normal test assertions using JUnit's Assert.assertThat(), so you get double use out of them.

                  这篇关于mockito 验证与 ArgumentCaptor 的交互的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:Mockito 和 Hamcrest:如何验证 Collection 参数的调用? 下一篇:mockito ArrayList&lt;String&gt;问题

                  相关文章

                  最新文章

                  1. <tfoot id='UHmX2'></tfoot>

                      <small id='UHmX2'></small><noframes id='UHmX2'>

                    1. <legend id='UHmX2'><style id='UHmX2'><dir id='UHmX2'><q id='UHmX2'></q></dir></style></legend>

                    2. <i id='UHmX2'><tr id='UHmX2'><dt id='UHmX2'><q id='UHmX2'><span id='UHmX2'><b id='UHmX2'><form id='UHmX2'><ins id='UHmX2'></ins><ul id='UHmX2'></ul><sub id='UHmX2'></sub></form><legend id='UHmX2'></legend><bdo id='UHmX2'><pre id='UHmX2'><center id='UHmX2'></center></pre></bdo></b><th id='UHmX2'></th></span></q></dt></tr></i><div id='UHmX2'><tfoot id='UHmX2'></tfoot><dl id='UHmX2'><fieldset id='UHmX2'></fieldset></dl></div>
                      • <bdo id='UHmX2'></bdo><ul id='UHmX2'></ul>