Issue
I have a code
csvWriter.writeCsv(new PrintWriter(csvPath), someList);
Is it possible to test string ".\xyz.csv" and TestUtil.getSomeList() exactly.
Below mockito code is giving me error "Argument(s) are different! Wanted" complaining about different PrintWriter objects.
verify(csvWriter, times(1)).writeCsv(new PrintWriter(".\\xyz.csv"), TestUtil.getSomeList());
It works if I change this to
verify(csvWriter, times(2)).writeCsv(any(PrintWriter.class), any(List.class));
But I would like to verify that ".\xyz.csv" and TestUtil.getSomeList() are matched exactly. Is it possible?
Solution
You can use ArgumentCaptor
to get the parameters you put to the writeCsv
method:
ArgumentCaptor<PrintWriter> writerCaptor = ArgumentCaptor.forClass(PrintWriter.class);
ArgumentCaptor<> listCaptor = ArgumentCaptor.forClass(List.class);
verify(csvWriter, times(2)).writeCsv(writerCaptor.capture, listCaptor.capture());
//Then you can check captured parameters:
List<...> actualList = listCaptor.getValue();
assertEquals(expectedList, actualList);
Sample : https://www.baeldung.com/mockito-argumentcaptor
Answered By - Anatolii Chub
Answer Checked By - David Goodson (JavaFixing Volunteer)