<legend id='t7ctX'><style id='t7ctX'><dir id='t7ctX'><q id='t7ctX'></q></dir></style></legend>

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

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

      1. Mockito 验证不再与任何模拟交互

        时间:2023-09-26

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

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

                  问题描述

                  在 Mockito 中,有没有办法验证我创建的任何模拟上没有更多交互?

                  In Mockito, is there a way to verify that there are no more interactions on any mock I have created?

                  例如:

                  public void test()
                  {
                    ...
                    TestObject obj = mock(TestObject);
                    myClass.test();
                    verifyNoMoreInteractionsWithMocks();  <-------
                  }
                  

                  有这样的方法吗?

                  推荐答案

                  自从 verifyNoMoreInteractions 获取一个对象数组,我们必须找到一种方法来获取所有创建的模拟.

                  Since verifyNoMoreInteractions take an array of object we have to find a way to get all the created mocks.

                  你可以创建这个类

                  public class MocksCollector {
                      private final List<Object> createdMocks;
                  
                      public MocksCollector() {
                          createdMocks = new LinkedList<Object>();
                          final MockingProgress progress = new ThreadSafeMockingProgress();
                          progress.setListener(new CollectCreatedMocks(createdMocks));
                      }
                  
                      public Object[] getMocks() {
                          return createdMocks.toArray();
                      }
                  }
                  

                  然后在你的测试中使用它:

                  and then use it in your test :

                      public class ATest {
                      private final MocksCollector mocksCollector = new MocksCollector();
                  
                      @Test
                      public void test() throws Exception {
                          A a1 = mock(A.class);
                          A a2 = mock(A.class);
                          A a3 = mock(A.class);
                          assertEquals("wrong amount of mocks", 3, mocksCollector.getMocks().length);
                          verifyNoMoreInteractions(mocksCollector.getMocks());
                          a3.doSomething();
                          verifyNoMoreInteractions(mocksCollector.getMocks()); // fail
                      }
                  }
                  

                  或带注释:

                  @RunWith(MockitoJUnitRunner.class)
                  public class A2Test {
                      private final MocksCollector mocksCollector = new MocksCollector();
                  
                      @Mock
                      private A a1;
                      @Mock
                      private A a2;
                      @Mock
                      private A a3;
                  
                      @Test
                      public void test() throws Exception {
                          assertEquals("wrong amount of mocks", 3, mocksCollector.getMocks().length);
                          verifyNoMoreInteractions(mocksCollector.getMocks());
                          a2.doSomething();
                          verifyNoMoreInteractions(mocksCollector.getMocks()); // fail
                      }
                  }
                  

                  它可以工作,但它增加了对内部模拟的依赖.

                  It works but it adds a dependency on mockito internal.

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

                  上一篇:如何验证具有不同参数的多个方法调用 下一篇:使用 PowerMock 模拟私有方法,但仍会调用底层方法

                  相关文章

                  最新文章

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

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

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

                    <tfoot id='u8QOh'></tfoot>
                      <legend id='u8QOh'><style id='u8QOh'><dir id='u8QOh'><q id='u8QOh'></q></dir></style></legend>