<legend id='3030M'><style id='3030M'><dir id='3030M'><q id='3030M'></q></dir></style></legend>

      <bdo id='3030M'></bdo><ul id='3030M'></ul>
    1. <small id='3030M'></small><noframes id='3030M'>

    2. <i id='3030M'><tr id='3030M'><dt id='3030M'><q id='3030M'><span id='3030M'><b id='3030M'><form id='3030M'><ins id='3030M'></ins><ul id='3030M'></ul><sub id='3030M'></sub></form><legend id='3030M'></legend><bdo id='3030M'><pre id='3030M'><center id='3030M'></center></pre></bdo></b><th id='3030M'></th></span></q></dt></tr></i><div id='3030M'><tfoot id='3030M'></tfoot><dl id='3030M'><fieldset id='3030M'></fieldset></dl></div>
      <tfoot id='3030M'></tfoot>
    3. mockito anyList 给定大小

      时间:2023-09-26

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

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

                本文介绍了mockito anyList 给定大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我正在使用 mockito 验证一个方法已被调用.方法:

                I'm verifying with mockito that a method has been called. The method:

                public void createButtons(final List<Button> buttonsConfiguration) {...}
                

                由于传递哪个列表并不重要,因此我验证该方法的调用如下:

                Since It doesn't matter which list is passed I verify that the method is called as follows:

                verify(mock).createButtons(Matchers.anyListOf(Button.class));
                

                但是,List 的大小很重要.因此,哪个 List 并不重要,但列表必须有 X 个元素.

                But, the size of the List is important. So, it doesn't matter which List but the list has to have X elements.

                这可能吗?

                推荐答案

                一种方法是使用 Captor

                One way is to use a Captor

                ArgumentCaptor<List> captor = ArgumentCaptor.forClass(List.class);
                verify(mock).createButtons(captor.capture());
                assertEquals(x, captor.getValue().size()); // if expecting single list
                assertEquals(x, captor.getValues().size()); // if expecting multiple lists
                

                请参阅 http://docs.mockito.googlecode.com/hg/org/mockito/Mockito.html#15 获取文档.

                您还可以使用自定义参数匹配器.该文档显示了一个完全符合您要求的示例:

                You could also use a custom argument matcher. The documentation shows an example that does exactly what you want:

                http://docs.mockito.googlecode.com/hg/org/mockito/ArgumentMatcher.html

                 class IsListOfTwoElements extends ArgumentMatcher<List> {
                     public boolean matches(Object list) {
                         return ((List) list).size() == 2;
                     }
                 }
                 
                 List mock = mock(List.class);
                 when(mock.addAll(argThat(new IsListOfTwoElements()))).thenReturn(true);
                 mock.addAll(Arrays.asList("one", "two"));
                 verify(mock).addAll(argThat(new IsListOfTwoElements()));
                

                例如,您还可以添加一个构造函数,以便指定所需的列表大小等.

                You could, for instance, also add a constructor so you can specify list size desired, etc.

                这篇关于mockito anyList 给定大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:如何处理“任何其他价值"?与 Mockito? 下一篇:无效的键码@java

                相关文章

                最新文章

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

                  • <bdo id='ZCRAM'></bdo><ul id='ZCRAM'></ul>
                1. <small id='ZCRAM'></small><noframes id='ZCRAM'>

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