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)
Lazy verification avoids verification for data that has a short life- time and is never read.
Mockito Verify methods are used to check that certain behavior happened. We can use Mockito verify methods at the end of the testing method code to make sure that specified methods are called.
@InjectMocks is the Mockito Annotation. It allows you to mark a field on which an injection is to be performed. Injection allows you to, Enable shorthand mock and spy injections. Minimize repetitive mock and spy injection.
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();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With