• <tfoot id='O36vo'></tfoot>

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

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

      1. 使用 Mockito 匹配特定类型的空列表

        时间:2023-09-25
        <legend id='NQVsN'><style id='NQVsN'><dir id='NQVsN'><q id='NQVsN'></q></dir></style></legend>

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

            <tfoot id='NQVsN'></tfoot>

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

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

                  本文介绍了使用 Mockito 匹配特定类型的空列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  我有一个需要几个参数的方法.其中之一是某个类的 List,null 是该参数的可接受值.

                  I have a method that takes a few parameters. One of them is a List of some class, and null is an acceptable value for this parameter.

                  public void doStuff(String string, @Nullable List<SomeClass> list) {
                      ...
                  }
                  

                  我想编写一个测试,使用 Mockito 来验证该方法是否以 null 作为参数调用.我尝试使用 isNull(List.class):

                  I want to write a test that uses Mockito to verify the method was called with null as a parameter. I tried using isNull(List.class):

                  MyClass myClass = ...
                  verify(myClass).doStuff(any(String.class), isNull(List.class));
                  

                  但这会产生警告:

                  未经检查的转换
                  需要:java.util.List
                  com.package.SomeClass>
                  找到:java.util.List

                  unchecked conversion
                  required: java.util.List< com.package.SomeClass>
                  found: java.util.List

                  如果列表不为空,我可以看到如何修复此警告:

                  I can see how to fix this warning in the event that the list is not null:

                  // this generates the same warning
                  verify(myClass).doStuff(any(String.class), any(List.class));
                  
                  // this does not generate the warning
                  verify(myClass).doStuff(any(String.class), Matchers.anyListOf(SomeClass.class)));
                  

                  但是,我似乎找不到将这两种方法结合在一起的方法.或者找到一种替代方法来完成我想做的事情.(除了用注释来抑制警告)

                  However, I can't seem to find a way to combine these two approaches together. Or find an alternate approach that accomplishes what I am trying to do. (Other than suppressing the warning with an annotation)

                  推荐答案

                  一般的答案是使用显式的泛型方法参数.

                  The general answer is to use an explicit generic method argument.

                  verify(myClass).doStuff(any(String.class), Matchers.<List<SomeClass>>isNull());
                  

                  补充说明:

                  • 实际上,警告并不能保护您免受任何伤害;nullnull,并且通过类型擦除,所有这些形式都将编译为相同的字节码.
                  • Java 8 可以从方法参数推断泛型类型,因此 isNull() 就足够了.
                  • Mockito 1.x 调用类 org.mockito.Matchers,而 Mockito 2.x 弃用该类以支持 org.mockito.ArgumentMatchers.在这两种情况下,匹配器方法都可以通过 org.mockito.Mockito 看到,但 static-methods-via-inheritance 在语义上很弱,可能会导致这些方法不显示在 IDE 中.
                  • 如果没有将 Matchers 作为静态参数,则无法指定 <List<SomeClass>>isNull().你可以做什么,如果你需要做很多,就是提取一个本地静态方法......

                  • In practice, the warning isn't protecting you from anything; null is null, and with type erasure all of these forms will compile to the same bytecode anyway.
                  • Java 8 can infer generic types from method arguments, so isNull() is sufficient there without cleverness.
                  • Mockito 1.x calls the class org.mockito.Matchers whereas Mockito 2.x deprecates that class in favor of org.mockito.ArgumentMatchers. In both cases the matcher methods are visible through org.mockito.Mockito, but static-methods-via-inheritance is semantically weak and can cause those methods not to show up in IDEs.
                  • There's no way to specify <List<SomeClass>>isNull() without the Matchers as a static argument. What you can do, if you need to do this a lot, is to extract a local static method...

                  private static List<SomeClass> nullSomeClassList() {
                    return isNull();  // Return value types can be inferred before Java 8.
                  }
                  
                  // elsewhere
                  verify(myClass).doStuff(any(String.class), nullSomeClassList());
                  

                  ...但无论你做什么,不要提取到字段.副作用在这里很重要.

                  ...but whatever you do, don't extract to a field instead. The side effects are what matters here.

                  这篇关于使用 Mockito 匹配特定类型的空列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:模棱两可的方法调用模拟 RestTemplate.exchange() 下一篇:如何使用 Mockito 和 JUnit 检查方法中的 if 语句?

                  相关文章

                  最新文章

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

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

                      <tfoot id='aocq5'></tfoot>