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

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

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

          <bdo id='cxM8Q'></bdo><ul id='cxM8Q'></ul>
      1. 通过 mockito 创建一个模拟列表

        时间:2023-05-29
      2. <tfoot id='TWf6c'></tfoot>
          <tbody id='TWf6c'></tbody>

        • <legend id='TWf6c'><style id='TWf6c'><dir id='TWf6c'><q id='TWf6c'></q></dir></style></legend>

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

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

              • <i id='TWf6c'><tr id='TWf6c'><dt id='TWf6c'><q id='TWf6c'><span id='TWf6c'><b id='TWf6c'><form id='TWf6c'><ins id='TWf6c'></ins><ul id='TWf6c'></ul><sub id='TWf6c'></sub></form><legend id='TWf6c'></legend><bdo id='TWf6c'><pre id='TWf6c'><center id='TWf6c'></center></pre></bdo></b><th id='TWf6c'></th></span></q></dt></tr></i><div id='TWf6c'><tfoot id='TWf6c'></tfoot><dl id='TWf6c'><fieldset id='TWf6c'></fieldset></dl></div>
                  本文介绍了通过 mockito 创建一个模拟列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我想创建一个模拟列表来测试以下代码:

                  I want to create a mocked list to test below code:

                   for (String history : list) {
                          //code here
                      }
                  

                  这是我的实现:

                  public static List<String> createList(List<String> mockedList) {
                  
                      List<String> list = mock(List.class);
                      Iterator<String> iterHistory = mock(Iterator.class);
                  
                      OngoingStubbing<Boolean> osBoolean = when(iterHistory.hasNext());
                      OngoingStubbing<String> osHistory = when(iterHistory.next());
                  
                      for (String history : mockedList) {
                  
                          osBoolean = osBoolean.thenReturn(true);
                          osHistory = osHistory.thenReturn(history);
                      }
                      osBoolean = osBoolean.thenReturn(false);
                  
                      when(list.iterator()).thenReturn(iterHistory);
                  
                      return list;
                  }
                  

                  但是当测试运行时,它会在以下行抛出异常:

                  But when the test run it throw exception at line:

                  OngoingStubbing<DyActionHistory> osHistory = when(iterHistory.next());
                  

                  详情:

                  org.mockito.exceptions.misusing.UnfinishedStubbingException: 
                  Unfinished stubbing detected here:
                  -> at org.powermock.api.mockito.PowerMockito.when(PowerMockito.java:495)
                  
                  E.g. thenReturn() may be missing.
                  Examples of correct stubbing:
                      when(mock.isOk()).thenReturn(true);
                      when(mock.isOk()).thenThrow(exception);
                      doThrow(exception).when(mock).someVoidMethod();
                  Hints:
                   1. missing thenReturn()
                   2. you are trying to stub a final method, you naughty developer!
                  

                  我该如何解决?谢谢

                  推荐答案

                  好吧,这是一件坏事.不要模拟列表;相反,模拟列表中的各个对象.请参阅 Mockito:模拟将在 for 循环中循环 以了解如何执行此操作.

                  OK, this is a bad thing to be doing. Don't mock a list; instead, mock the individual objects inside the list. See Mockito: mocking an arraylist that will be looped in a for loop for how to do this.

                  另外,您为什么要使用 PowerMock?你似乎没有做任何需要 PowerMock 的事情.

                  Also, why are you using PowerMock? You don't seem to be doing anything that requires PowerMock.

                  但问题的真正原因是您在完成存根之前在两个不同的对象上使用了 when.当您调用 when 并提供您尝试存根的方法调用时,您在 Mockito 或 PowerMock 中所做的下一件事就是指定调用该方法时会发生什么 - 也就是说,执行 thenReturn 部分.每次对 when 的调用必须跟一个且只有一个对 thenReturn 的调用,然后再对 when 进行任何调用.您对 when 进行了两次调用而没有调用 thenReturn - 这是您的错误.

                  But the real cause of your problem is that you are using when on two different objects, before you complete the stubbing. When you call when, and provide the method call that you are trying to stub, then the very next thing you do in either Mockito OR PowerMock is to specify what happens when that method is called - that is, to do the thenReturn part. Each call to when must be followed by one and only one call to thenReturn, before you do any more calls to when. You made two calls to when without calling thenReturn - that's your error.

                  这篇关于通过 mockito 创建一个模拟列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:多个级别的@Mock 和@InjectMocks 下一篇:存根和模拟时的区别

                  相关文章

                    • <bdo id='nsthb'></bdo><ul id='nsthb'></ul>

                      <tfoot id='nsthb'></tfoot>

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

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

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