Java Mockito - 通缉但未调用:实际上,与此模拟的交互为零
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/28089601/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock
提问by Arthur Eirich
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?
我没有最终方法,我使用模拟的 pdf writer 作为参数。我还需要做什么才能通过测试?
UPDATEBelow 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 2After replacing the mocked WithDefinitions object with the real instance I get the following output:
更新 2用真实实例替换模拟的 WithDefinitions 对象后,我得到以下输出:
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)
采纳答案by JB Nizet
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.
您正在嘲笑您正在测试的对象。这是没有意义的。您应该创建一个真正的 WithDefinitions 对象并调用其真正的方法来测试它。如果你模拟它,根据定义,它的所有方法都会被什么都不做的模拟实现所取代。
Replace
代替
WithDefinitions withDefinitions = Mockito.mock(WithDefinitions.class);
by something like
通过类似的东西
WithDefinitions withDefinitions = new WithDefinitions();