使用Mockito测试

时间:2016-07-14 14:23:01

标签: java spring mockito

这是我的代码,我想知道使用Mockito进行测试的最佳方法是什么,因为我使用new关键字创建了几个对象。谁能指导我?

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

1 个答案:

答案 0 :(得分: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
}