I want to validate the logging made by a helper class, which calls a method with some varargs.
I am using Mockito (1.10.19) to mock the actual logger, and to verify the mocked method is called as expected.
I use an ArgumentCaptor to validate the arguments.
The Mockito.verify validates the number of times the mocked method is called, however, the ArgumentCaptor.getAllValues is returning a single array with all the parameters of all the method calls.
Here is a sample code:
interface Logger
{
void info(Object... params);
}
@Mock
Logger logger;
public void logMatrix(String[][] matrix)
{
for (int column = 0; column < matrix.length; column++)
{
logger.info(column, " : ", matrix[column]);
}
}
@Test
public void givenMatrix_whenLogMatrix_thenLogsEachRow() throws Exception
{
String[][] matrix = {
{"a", "b"},
{"c", "d"}
};
ArgumentCaptor<Object[]> captor = ArgumentCaptor.forClass(Object[].class);
logMatrix(matrix);
// verify the mocked method is called twice
Mockito.verify(logger, times(2)).info(captor.capture());
// verify the contents of the calls: expecting two arrays, one for each call
assertThat(captor.getAllValues()).hasSize(2);
// fails !
}
The failure is:
java.lang.AssertionError:
Expected size:<2> but was:<6> in:
<[0, " : ", ["a", "b"], 1, " : ", ["c", "d"]]>
at TestLogHelper.givenMatrix_whenLogMatrix_thenLogsEachRow(TestLogHelper.java:72)
...
Is it a misuse? or a bug in mockito ?