簡體   English   中英

Mockito - 通緝但沒有被引用:實際上,與這個模擬沒有交互

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

我知道已經有至少兩個相同的問題,但我仍然無法弄清楚為什么我得到例外。 我需要對這個方法進行單元測試:

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

被稱為。

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

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

最后我的單元測試代碼:

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編寫器作為參數。 為了讓測試通過,我還需要做些什么?

更新以下是異常消息:

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對象后,我得到以下輸出:

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對象並調用其真實方法來測試它。 如果你模仿它,根據定義,它的所有方法都被無效的模擬實現所取代。

更換

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

通過類似的東西

WithDefinitions withDefinitions = new WithDefinitions();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM