Mockito - 通缉但没有被引用:实际上,与这个模拟没有交互

Art*_*ich 3 java unit-testing junit4 mockito

我知道已经有至少两个相同的问题,但我仍然无法弄清楚为什么我得到例外.我需要对这个方法进行单元测试:

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();
}
Run Code Online (Sandbox Code Playgroud)

并验证

canvas.addTemplate(page, posX, posY); 
Run Code Online (Sandbox Code Playgroud)

被称为.

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

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);
    }
Run Code Online (Sandbox Code Playgroud)

最后我的单元测试代码:

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)
    );
Run Code Online (Sandbox Code Playgroud)

我没有最终方法,我使用模拟pdf编写器作为参数.为了让测试通过,我还需要做些什么?

更新 以下是异常消息:

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.
Run Code Online (Sandbox Code Playgroud)

更新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)
Run Code Online (Sandbox Code Playgroud)

JB *_*zet 15

你在嘲笑你正在测试的对象.这是没有意义的.您应该创建一个真正的WithDefinitions对象并调用其真实方法来测试它.如果你模仿它,根据定义,它的所有方法都被无效的模拟实现所取代.

更换

WithDefinitions withDefinitions = Mockito.mock(WithDefinitions.class);
Run Code Online (Sandbox Code Playgroud)

通过类似的东西

WithDefinitions withDefinitions = new WithDefinitions();
Run Code Online (Sandbox Code Playgroud)

  • 好吧,错误信息是自解释的.测试期望通过62.36221,实际传递的是48.18898.所以,代码有错误,或者测试有bug.修理它. (2认同)