<bdo id='TPxNZ'></bdo><ul id='TPxNZ'></ul>
    1. <legend id='TPxNZ'><style id='TPxNZ'><dir id='TPxNZ'><q id='TPxNZ'></q></dir></style></legend>

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

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

        Mockito 和 Hamcrest:如何验证 Collection 参数的调用?

        时间:2023-10-01
      3. <small id='Isb4a'></small><noframes id='Isb4a'>

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

                    <tbody id='Isb4a'></tbody>
                  <tfoot id='Isb4a'></tfoot>
                • 本文介绍了Mockito 和 Hamcrest:如何验证 Collection 参数的调用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我遇到了 Mockito 和 Hamcrest 的泛型问题.

                  I'm running into a generics problem with Mockito and Hamcrest.

                  请假设如下界面:

                  public interface Service {
                      void perform(Collection<String> elements);
                  }
                  

                  还有下面的测试片段:

                  Service service = mock(Service.class);
                  
                  // ... perform business logic
                  
                  verify(service).perform(Matchers.argThat(contains("a", "b")));
                  

                  所以我想验证我的业务逻辑是否真的使用包含a"和b"的集合来调用服务.

                  So I want to verify that my business logic actually called the service with a collection that contains "a" and "b" in that order.

                  但是contains(...)的返回类型是Matcher,所以 Matchers.argThat(...) 在我的情况下返回 Iterable<String> ,这自然不适用于所需集合.

                  However, the return type of contains(...) is Matcher<Iterable<? extends E>>, so Matchers.argThat(...) returns Iterable<String> in my case, which naturally does not apply to the required Collection<String>.

                  我知道我可以使用 Hamcrest hasItem and Mockito verify 中建议的参数捕获器不一致,但我非常不想这样做.

                  I know that I could use an argument captor as proposed in Hamcrest hasItem and Mockito verify inconsistency, but I would very much like not to.

                  任何建议!谢谢!

                  推荐答案

                  你可以写

                  verify(service).perform((Collection<String>) Matchers.argThat(contains("a", "b")));
                  

                  从编译器的角度来看,这是将 Iterable<String> 转换为 Collection 这很好,因为后者是前任的.在运行时,argThat 将返回 null,因此可以在没有 ClassCastException 的情况下将其传递给 perform.重要的一点是,匹配器进入 Mockito 的内部参数结构进行验证,这就是 argThat 所做的.

                  From the compiler's point of view, this is casting an Iterable<String> to a Collection<String> which is fine, because the latter is a subtype of the former. At run time, argThat will return null, so that can be passed to perform without a ClassCastException. The important point about it is that the matcher gets onto Mockito's internal structure of arguments for verification, which is what argThat does.

                  这篇关于Mockito 和 Hamcrest:如何验证 Collection 参数的调用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  <tfoot id='U4ZF5'></tfoot>

                    <bdo id='U4ZF5'></bdo><ul id='U4ZF5'></ul>

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

                      <legend id='U4ZF5'><style id='U4ZF5'><dir id='U4ZF5'><q id='U4ZF5'></q></dir></style></legend>
                        • <i id='U4ZF5'><tr id='U4ZF5'><dt id='U4ZF5'><q id='U4ZF5'><span id='U4ZF5'><b id='U4ZF5'><form id='U4ZF5'><ins id='U4ZF5'></ins><ul id='U4ZF5'></ul><sub id='U4ZF5'></sub></form><legend id='U4ZF5'></legend><bdo id='U4ZF5'><pre id='U4ZF5'><center id='U4ZF5'></center></pre></bdo></b><th id='U4ZF5'></th></span></q></dt></tr></i><div id='U4ZF5'><tfoot id='U4ZF5'></tfoot><dl id='U4ZF5'><fieldset id='U4ZF5'></fieldset></dl></div>
                              <tbody id='U4ZF5'></tbody>