jeudi 22 janvier 2015

Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock

I know there are already at least two same questions asked, but I still can't figure out why I am getting the exception. I need to unit test this method:



void setEyelet(final PdfWriter printPdf, final float posX, final float posY) {

InputStream is = WithDefinitions.class.getResourceAsStream(RES_EYELET); //RES_EYELET is a pdf.
PdfContentByte canvas = printPdf.getDirectContent();

PdfReader reader = new PdfReader(is);
PdfImportedPage page = printPdf.getImportedPage(reader, 1);
canvas.addTemplate(page, posX, posY);
reader.close();
}


This method is nested in an another method:



void computeEyelets(final PdfWriter printPdf) {
float lineLeft = borderLeft + EYELET_MARGIN;
float lineRight = printPdfWidth - borderRight - EYELET_MARGIN - EYELET_SIZE;
float lineTop = printPdfHeight - borderTop - EYELET_MARGIN - EYELET_SIZE;
float lineBottom = borderBottom + EYELET_MARGIN;
float eyeletDistMinH = 20;
if (eyeletDistMinH != 0 || eyeletDistMinV != 0) {
setEyelet(printPdf, lineLeft, lineBottom);
}


And finally my unit test code:



public void computeEyeletsNoMirror() {
PdfWriter pdfWriter = Mockito.mock(PdfWriter.class);
PdfContentByte pdfContentByte = Mockito.mock(PdfContentByte.class);
Mockito.when(pdfWriter.getDirectContent()).thenReturn(pdfContentByte);
WithDefinitions withDefinitions = Mockito.mock(WithDefinitions.class);
float lineLeft = BORDER_LEFT + EYELET_MARGIN;
float lineBottom = BORDER_BOTTOM + EYELET_MARGIN;

withDefinitions.setEyeletDistMinH(20);
withDefinitions.setEyeletDistMinV(20);
withDefinitions.setMirror(false);

withDefinitions.computeEyelets(pdfWriter);

Mockito.verify(pdfContentByte).addTemplate(
Mockito.any(PdfImportedPage.class),
Mockito.eq(lineLeft),
Mockito.eq(lineBottom)
);


I have no final methods, I use mocked pdf writer as a parameter. What do I need else to do to get the test passing?


Aucun commentaire:

Enregistrer un commentaire