java - is there Mockito eq matcher for varargs array? -
i have problem when trying match array passed parameter method receives varargs array.
the anyvararg() matcher mentioned in other questions/answers doesn't work me because want create sure provided array 1 need.
i reduced problem illustration easier understand , abstracts problem (my real issue production code , has busines logic confusing purpose of 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() { // pass eq works normal array doreturn(expected).when(target).tolist(mockito.eq(objectarray)); assert.assertequals(expected, target.makelist(objectarray)); } @test public void testmakelist1() { // 1 fails eq not working varargs doreturn(expected).when(target).tolist1(mockito.eq(objectarray)); assert.assertequals(expected, target.makelist1(objectarray)); } @test public void testmakelistwitharyeq() { // fails, aryeq not working varargs doreturn(expected).when(target).tolist1(additionalmatchers.aryeq(objectarray)); assert.assertequals(expected, target.makelist1(objectarray)); } private class testtarget { public list<object> makelist(object[] objects) { homecoming tolist(objects); } public list<object> makelist1(object[] objects) { homecoming tolist1(objects); } protected list<object> tolist(object[] objs) { homecoming null; // not implemented "intentionally" } protected list<object> tolist1(object... objs) { homecoming null; // not implemented "intentionally" } } } when run test cases in class, first test case pass not other two, neither using eq nor using aryeq. showing next trace:
java.lang.assertionerror: expected:<[java.lang.object@56d5e457, java.lang.object@7482384a]> was:<null> @ org.junit.assert.fail(assert.java:88) @ org.junit.assert.failnotequals(assert.java:743) @ org.junit.assert.assertequals(assert.java:118) @ org.junit.assert.assertequals(assert.java:144) ... this happens because eq matcher not working varargs arrays, there alternative eq matcher utilize case?
ok, think reply here requires custom built matcher, can implemented in unit test so:
private class myvarargmatcher extends argumentmatcher<object[]> implements varargmatcher { private object[] expectedvalues; myvarargmatcher(object... expectedvalues) { this.expectedvalues = expectedvalues; } @override public boolean matches(object varargargument) { homecoming new equalsbuilder() .append(expectedvalues, varargargument) .isequals(); } } then, in testmakelist1() alter first line this:
mockito.doreturn(expected).when(target).tolist1(mockito.argthat(new myvarargmatcher(objectarray))); sources: how match varargs in mockito http://maciejmadej.blogspot.com/2011/11/capturing-varargs-argument-using-custom.html
java junit mockito
No comments:
Post a Comment