Ho un problema quando provo ad abbinare un array che viene passato come parametro a un metodo che riceve un array varargs.c'è Mockito eq matcher per varargs array?
Il matcher anyVararg() menzionato in altre domande/risposte non funziona per me perché voglio essere sicuro che l'array fornito sia quello di cui ho bisogno.
ho ridotto il problema a questo esempio che è più facile da capire e astrae il problema (il mio vero problema è il codice di produzione e ha logica busines quindi sarebbe fonte di confusione per lo scopo di questa domanda):
@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"
}
}
}
Quando eseguo i test case nella classe, il primo test case passerà ma non gli altri due, né usando eq né usando aryEq. Mostrando la seguente traccia:
java.lang.AssertionError: expected:<[[email protected], [email protected]384a]> 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)
...
Ciò accade perché il matcher eq non funziona con varargs array, c'è qualche alternativa al matcher eq per questo caso d'uso?
Come ho accennato nella domanda, il codice mostrato è un esempio che rende facile vedere come il matcher non funziona con varargs. Il codice in cui ho un problema è di proprietà della mia azienda e ha un codice di stile di produzione con logica aziendale che non posso condividere. La domanda è se c'è un equivalente del matcher che funzionerà con varargs – raspacorp
Ok ho creato un matcher personalizzato da usare, per favore fatemi sapere se funziona. – JLewkovich
Sembra che la risposta sia: no non ci sono attualmente equivalenti equivalenti eq in Mockito per varargs, ma possiamo crearne uno uguale a quello che tu e il post di blog che hai postato. Ho implementato il mio e aggiunto anche un metodo che estrae la chiamata matcher in modo da poter scrivere eqVararg (objectArray) in questo modo: private Object [] eqVararg (Object ... vararg) {return Mockito.argThat (new VarargsMatcher (vararg)); } – raspacorp