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

    1. <legend id='4zPnU'><style id='4zPnU'><dir id='4zPnU'><q id='4zPnU'></q></dir></style></legend>
        <bdo id='4zPnU'></bdo><ul id='4zPnU'></ul>
    2. <tfoot id='4zPnU'></tfoot>

      <small id='4zPnU'></small><noframes id='4zPnU'>

        Mockito:使用有界通配符返回类型的存根方法

        时间:2023-09-25
        <i id='l7nOL'><tr id='l7nOL'><dt id='l7nOL'><q id='l7nOL'><span id='l7nOL'><b id='l7nOL'><form id='l7nOL'><ins id='l7nOL'></ins><ul id='l7nOL'></ul><sub id='l7nOL'></sub></form><legend id='l7nOL'></legend><bdo id='l7nOL'><pre id='l7nOL'><center id='l7nOL'></center></pre></bdo></b><th id='l7nOL'></th></span></q></dt></tr></i><div id='l7nOL'><tfoot id='l7nOL'></tfoot><dl id='l7nOL'><fieldset id='l7nOL'></fieldset></dl></div>

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

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

          <tbody id='l7nOL'></tbody>

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

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

                  本文介绍了Mockito:使用有界通配符返回类型的存根方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  考虑这段代码:

                  public class DummyClass {
                      public List<? extends Number> dummyMethod() {
                          return new ArrayList<Integer>();
                      }
                  }
                  

                  public class DummyClassTest {
                      public void testMockitoWithGenerics() {
                          DummyClass dummyClass = Mockito.mock(DummyClass.class);
                          List<? extends Number> someList = new ArrayList<Integer>();
                          Mockito.when(dummyClass.dummyMethod()).thenReturn(someList); //Compiler complains about this
                      }
                  }
                  

                  编译器抱怨试图为 dummyMethod() 存根的行.关于如何处理返回具有有界通配符类型的存根方法的任何指针?

                  The compiler complains about the line that's trying to stub the behavior for dummyMethod(). Any pointers on how one goes about stubbing methods that return a type with bounded wild-cards?

                  推荐答案

                  你也可以使用非类型安全的方法 doReturn 为此,

                  You can also use the non-type safe method doReturn for this purpose,

                  @Test
                  public void testMockitoWithGenerics()
                  {
                      DummyClass dummyClass = Mockito.mock(DummyClass.class);
                      List<? extends Number> someList = new ArrayList<Integer>();
                  
                      Mockito.doReturn(someList).when(dummyClass).dummyMethod();
                  
                      Assert.assertEquals(someList, dummyClass.dummyMethod());
                  }
                  

                  作为 讨论 在 Mockito 的 google 组上.

                  as discussed on Mockito's google group.

                  虽然这比 thenAnswer 简单,但再次注意它不是类型安全的.如果您担心类型安全,millhouse 的answer 是正确的.

                  While this is simpler than thenAnswer, again note that it is not type safe. If you're concerned about type safety, millhouse's answer is correct.

                  要清楚,这是观察到的编译器错误,

                  To be clear, here's the observed compiler error,

                  OngoingStubbing) 方法扩展号码>>不适用于参数 (List)

                  我相信编译器在 when 调用期间分配了第一个通配符类型,然后无法确认 thenReturn 调用中的第二个通配符类型相同.

                  I believe the compiler has assigned the first wildcard type during the when call and then cannot confirm that the second wildcard type in the thenReturn call is the same.

                  thenAnswer 似乎没有遇到此问题,因为它接受通配符类型,而 thenReturn 采用必须捕获的非通配符类型.来自 Mockito 的 OngoingStubbing,

                  It looks like thenAnswer doesn't run into this issue because it accepts a wildcard type while thenReturn takes a non-wildcard type, which must be captured. From Mockito's OngoingStubbing,

                  OngoingStubbing<T> thenAnswer(Answer<?> answer);
                  OngoingStubbing<T> thenReturn(T value);
                  

                  这篇关于Mockito:使用有界通配符返回类型的存根方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:莫基托.验证方法参数 下一篇:当我运行 mockito 测试时发生 WrongTypeOfReturnValue 异

                  相关文章

                  最新文章

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

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

                      <tfoot id='myWK7'></tfoot>

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