2016-07-14 5 views
0

ここに私のコードがあります。私は新しいキーワードを使っていくつかのオブジェクトを作成しているので、Mockitoを使ってテストする最良の方法は何かを知りたいと思います。誰も私を導くことができますか?Mockitoを使ったテスト

public static PDDocument generatePDF(final String reportString, final String requestId) throws IOException { 

     final PDDocument document = new PDDocument(); 

     final byte[] byteStr = reportString.getBytes(StandardCharsets.UTF_8); 
     final String str = new String(byteStr, 
       StandardCharsets.UTF_8); 

     final BufferedReader reader = new BufferedReader(new StringReader(str)); 

     try { 

      // PDF box ceremony 

      final TextToPDF textToPdf = new TextToPDF(); 
      textToPdf.setFont(PDType1Font.COURIER); 
      textToPdf.setFontSize(10); 

      textToPdf.createPDFFromText(document, reader); 

      reader.close(); 
     } catch (final IOException ioException) { 
      LOGGER.error("IO Exception while generating PDF for request id " + requestId, ioException.getMessage()); 
      throw ioException; 
     } catch (final Exception e) { 
      LOGGER.error("Exception while generating PDF for request id " + requestId, e.getMessage()); 
      throw e; 
     } finally { 
      reader.close(); 
     } 
     return document; 
} 
+0

私の推測では、あなたがApacheのPDFボックスを使用していることですあなたじゃない?あなたが提供する他のクラスへの参照は表示されませんが、フレームワーククラスに対してのみ参照されるため、私は尋ねています。 –

+0

はいApache PDF box apiを使用しています。 – Rakesh

答えて

0

Mockitoは、テストするクラス/メソッドの共同作業者を模擬するためのものです。あなたが所有するタイプを模倣するためだけに使用されるべきであることに注意してください。 この場合、実際にはMockitoは必要ありません。あなたはMockitoを使用することができているあなたに似 たとえば、この次のとおりです。この場合は

class PDFGenerator { 

    private ITextToPdf textToPdf; // This is an hypotetical interface provided by you, for example used as a wrapper to easily change the underling framework 

    public void setTextToPdf(ITextToPdf textToPdf) { 
     this.textToPdf = textToPdf; 
    } 

    public static PDDocument generatePDF(final String reportString, final String requestId) throws IOException { 

     final byte[] byteStr = reportString.getBytes(StandardCharsets.UTF_8); 
     final String str = new String(byteStr, 
      StandardCharsets.UTF_8); 

      final BufferedReader reader = new BufferedReader(new StringReader(str)); 

     try { 

      IDocument document = textToPdf.createPDFFromText(reader); 

      reader.close(); 

      return document; 
     } catch (final IOException ioException) { 
      LOGGER.error("IO Exception while generating PDF for request id " + requestId, ioException.getMessage()); 
      throw ioException; 
     } catch (final Exception e) { 
      LOGGER.error("Exception while generating PDF for request id " + requestId, e.getMessage()); 
      throw e; 
     } finally { 
      reader.close(); 
     } 

    } 
} 

、テストは次のようになります。

@Test 
public void testGeneratePdf() throws Exception { 
    ITextToPdf textToPdfMock Mockito.mock(ITextToPdf.class); 
    PDFGenerator pdfGenerator = new PDFGenerator(); 
    pdfGenerator.setTextToPdf(textToPdfMock); 

    Mockito.when(textToPdfMock.createPDFFromText(Mockito.any())).thenReturn(something); 
    IDocument generatedDocument = pdfGenerator.generatePDF(createReportString(), "TestId"); 

    Mockito.verify(textToPdfMock, Mockito.times(1)).createPDFFromText(Mockito.any()); 
    Mockito.verifyNoMoreInteractions(textToPdfMock); 
    // Do also some standard junit asserts on the generatedDocument 
} 
+0

ありがとうございます。はい、私はそれがサードパーティーのAPIであることに同意します、実際にはそれは必要ありませんMockito。私はあなたが与えている例を試してみます..ありがとう – Rakesh

関連する問題