Yet another JUnit test pattern

Testing document templates: generate PDFs in a JUnit test and open them up in Acrobat for immediate visual feedback. Use a random temporary file to avoid file locking.

protected final void renderDocument(String template, Object model)
        throws IOException {
    File folder = new File("target/pdf"); folder.mkdir();
    File pdf = File.createTempFile("test", ".pdf", folder);
    FileOutputStream fos = new FileOutputStream(pdf);
    try {
        new Document()
    } finally {
    Assert.assertTrue(pdf.length() > 0);
    if (Desktop.isDesktopSupported()) {

Document uses a Freemarker template and the passed-in model to generate XSL-FO, which in turn is passed to Apache FOP for PDF generation.


Leave a Reply