Mockito-想要但未被调用:实际上,与该模拟的交互为零 [英] Mockito - Wanted but not invoked: Actually, there were zero interactions with this mock

查看:237
本文介绍了Mockito-想要但未被调用:实际上,与该模拟的交互为零的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道至少已经问了两个相同的问题,但是我仍然不知道为什么我会得到例外. 我需要对该方法进行单元测试:

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

并确认

canvas.addTemplate(page, posX, posY); 

被叫.

此方法嵌套在另一个方法中:

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

我没有最终方法,我使用模拟的pdf writer作为参数.我还需要做些什么才能使测试通过?

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?

更新 以下是异常消息:

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.

更新2 用真实实例替换模拟的WithDefinitions对象后,我得到以下输出:

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)

推荐答案

您正在嘲笑要测试的对象.这是没有意义的.您应该创建一个真实的WithDefinitions对象并调用其real方法对其进行测试.如果按照定义进行模拟,则其所有方法都将被不执行任何操作的模拟实现所替代.

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 withDefinitions = Mockito.mock(WithDefinitions.class);

通过类似的方式

WithDefinitions withDefinitions = new WithDefinitions();

这篇关于Mockito-想要但未被调用:实际上,与该模拟的交互为零的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆