2015-01-22 2 views
3

적어도 두 가지 동일한 질문이 있지만 이미 예외가 발생하는 이유를 알 수 없습니다. 내가 단위 테스트에이 방법이 필요합니다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(); 
} 

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 작가를 사용하여, 어떤 최종적인 방법이 없습니다. 검사를 통과하기 위해해야 ​​할 일은 무엇입니까? 2 교체 후 조롱 WithDefinitions 나는 다음과 같은 출력을 얻을 실제 인스턴스와 객체

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 : 다음은 UPDATE

는 예외 메시지입니다

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) 
+0

같은 의해

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

를 교체? – Stephan

답변

13

당신 당신이 테스트하고있는 객체를 조롱하고 있습니다. 그건 말이 안돼. 실제 WithDefinitions 객체를 생성하고 실제 메소드를 호출하여 테스트해야합니다. 당신이 그것을 조롱한다면, 정의에 따르면, 모든 메소드는 아무것도하지 않는 모의 구현으로 대체됩니다.

는 수 당신이 게시물에 수 (호출 스택 포함)을 제외하고 과거

WithDefinitions withDefinitions = new WithDefinitions(); 
+0

내 두번째 업데이트보기 –

+2

글쎄, 오류 메시지 자체 설명입니다. 이 테스트에서는 62.36221이 전달되고 실제로 전달되는 것은 48.18898입니다. 따라서 코드에 버그가 있거나 테스트에 버그가 있습니다. 고쳐. –

+0

@JBNizet은 SPY를 사용하여 가능하지 않아야합니까? 스파이를 사용하고 있는데 실제 메소드는 무시되지만 검증은 실패합니다. – karate

관련 문제