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

匿名 (未验证) 提交于 2019-12-03 00:56:02

问题:

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(); } 

and verify that

canvas.addTemplate(page, posX, posY);  

was called.

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?

UPDATE Below is the exception message:

Wanted but not invoked:  pdfContentByte.addTemplate(   <any>,   62.36221,   62.36221 ); -> at ...tools.pdf.superimpose.WithDefinitionsTest.computeEyeletsNoMirror(WithDefinitionsTest.java:336) Actually, there were zero interactions with this mock. 

UPDATE 2 After replacing the mocked WithDefinitions object with the real instance I get the following output:

Argument(s) are different! Wanted: pdfContentByte.addTemplate(   <any>,   62.36221,   62.36221 ); -> at ...tools.pdf.superimpose.WithDefinitionsTest.computeEyeletsNoMirror(WithDefinitionsTest.java:336) Actual invocation has different arguments: pdfContentByte.addTemplate(   null,   48.18898,   48.18898 ); -> at ...tools.pdf.superimpose.WithDefinitions.setEyelet(WithDefinitions.java:850) 

回答1:

You are mocking the object that you're testing. That makes no sense. You should create a real WithDefinitions object and call its real method to test it. If you mock it, by definition, all its methods are replaced by mock implementations that do nothing.

Replace

WithDefinitions withDefinitions = Mockito.mock(WithDefinitions.class); 

by something like

WithDefinitions withDefinitions = new WithDefinitions(); 


易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!