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

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

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

      可变参数数组是否有 Mockito eq 匹配器?

      时间:2023-09-25

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

              <tfoot id='rKkLx'></tfoot>
              • <bdo id='rKkLx'></bdo><ul id='rKkLx'></ul>

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

                  <tbody id='rKkLx'></tbody>
                本文介绍了可变参数数组是否有 Mockito eq 匹配器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                问题描述

                我在尝试匹配作为参数传递给接收可变参数数组的方法的数组时遇到问题.

                I have a problem when trying to match an array that is passed as a parameter to a method that receives a varargs array.

                其他问题/答案中提到的 anyVararg() 匹配器对我不起作用,因为我想确保提供的数组是我需要的.

                我将问题简化为这个更容易理解和抽象问题的示例(我真正的问题是生产代码并且具有业务逻辑,因此对于这个问题的目的来说它会令人困惑):

                I reduced the problem to this example which is easier to understand and abstracts the problem (my real issue is production code and has busines logic so it would be confusing for the purpose of this question):

                @RunWith(MockitoJUnitRunner.class)
                public class UnitTest {
                    private Object[] objectArray;
                    private List<Object> expected;
                    private TestTarget target;
                
                    @Before
                    public void setUp() {
                        objectArray = new Object[]{ new Object() };
                        expected = Arrays.asList(new Object(), new Object());
                        target = Mockito.spy(new TestTarget());
                    }
                
                    @Test
                    public void testMakeList() { // this pass as eq works well with normal array
                        doReturn(expected).when(target).toList(Mockito.eq(objectArray));
                        Assert.assertEquals(expected, target.makeList(objectArray));
                    }
                
                    @Test
                    public void testMakeList1() { // this one fails as eq is not working with varargs
                        doReturn(expected).when(target).toList1(Mockito.eq(objectArray));
                        Assert.assertEquals(expected, target.makeList1(objectArray));
                    }
                
                    @Test
                    public void testMakeListWithAryEq() { // fails, aryEq is not working with varargs
                        doReturn(expected).when(target).toList1(AdditionalMatchers.aryEq(objectArray));
                        Assert.assertEquals(expected, target.makeList1(objectArray));
                    }
                
                    private class TestTarget {
                        public List<Object> makeList(Object[] objects) {
                            return toList(objects);
                        }
                
                        public List<Object> makeList1(Object[] objects) {
                            return toList1(objects);
                        }
                
                        protected List<Object> toList(Object[] objs) {
                            return null;  // Not implemented "Intentionally"
                        }
                
                        protected List<Object> toList1(Object... objs) {
                            return null;  // Not implemented "Intentionally"
                        }
                    }
                }
                

                当我在课堂上运行测试用例时,第一个测试用例会通过,但其他两个不会通过,既不使用 eq 也不使用 aryEq.显示以下跟踪:

                When I run the test cases in the class, the first test case will pass but not the other two, neither using eq nor using aryEq. Showing the following trace:

                java.lang.AssertionError: expected:<[java.lang.Object@56d5e457, java.lang.Object@7482384a]> but was:<null>
                    at org.junit.Assert.fail(Assert.java:88)
                    at org.junit.Assert.failNotEquals(Assert.java:743)
                    at org.junit.Assert.assertEquals(Assert.java:118)
                    at org.junit.Assert.assertEquals(Assert.java:144)
                    ...
                

                发生这种情况是因为 eq 匹配器不适用于 varargs 数组,对于这个用例,是否有任何替代 eq 匹配器的方法?

                This happens because the eq matcher is not working with varargs arrays, is there any alternative to the eq matcher for this use case?

                推荐答案

                好的,我认为这里的答案需要一个自定义构建的匹配器,可以在您的单元测试中这样实现:

                Ok, I think the answer here requires a custom built matcher, which can be implemented in your unit test as so:

                private class MyVarargMatcher extends ArgumentMatcher<Object[]> implements VarargMatcher {
                    private Object[] expectedValues;
                
                    MyVarargMatcher(Object... expectedValues) {
                        this.expectedValues = expectedValues;
                    }
                
                    @Override
                    public boolean matches(Object varargArgument) {
                        return new EqualsBuilder()
                        .append(expectedValues, varargArgument)
                        .isEquals();
                    }
                }
                

                然后,在 testMakeList1() 中将第一行更改为:

                Then, in testMakeList1() change the first line to this:

                Mockito.doReturn(expected).when(target).toList1(Mockito.argThat(new MyVarargMatcher(objectArray)));
                

                来源:
                如何在 Mockito 中正确匹配可变参数
                http://maciejmadej.blogspot.com/2011/11/capturing-varargs-argument-using-custom.html

                这篇关于可变参数数组是否有 Mockito eq 匹配器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                上一篇:java.lang.LinkageError: ClassCastException 下一篇:使用 PowerMockito 模拟 getClass 方法

                相关文章

                最新文章

                  <bdo id='OZPX1'></bdo><ul id='OZPX1'></ul>
                <tfoot id='OZPX1'></tfoot>
              • <legend id='OZPX1'><style id='OZPX1'><dir id='OZPX1'><q id='OZPX1'></q></dir></style></legend>

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

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