Chapter 2. Quickstart

Quickstart

Let's assume you have a some programs that generates PDF documents and you want to make sure that the programs do what they should. Further expect that your test document has exactly one page and contains the greeting Thank you for using our services. and the value 30.34 Euro for the total bill. Then it's easy to check these requirements:

@Test
public void hasOnePage() throws Exception {
  String filename = "quickstart/quickstartDemo_en.pdf";
  AssertThat.document(filename)
            .hasNumberOfPages(1)
  ;
}

@Test
public void letterHasExpectedRegards() throws Exception {
  String filename = "quickstart/quickstartDemo_en.pdf";
  String expectedRegards = "Thank you for using our services.";
  AssertThat.document(filename)
            .restrictedTo(LAST_PAGE)
            .hasText()
            .containing(expectedRegards)
  ;
}

@Test 
public void hasExpectedCharge() throws Exception {
  String filename = "quickstart/quickstartDemo_en.pdf";
  int leftX  =  172;  // in millimeter
  int upperY =  178;
  int width  =   20;
  int height =    9;
  PageRegion regionInvoiceTotal = new PageRegion(leftX, upperY, width, height);
  
  AssertThat.document(filename)
            .restrictedTo(FIRST_PAGE)
            .restrictedTo(regionInvoiceTotal)
            .hasText()
            .containing("30,34 Euro") // This is really expected.
            .containing("29,89 Euro") // Let's see an error message :-)
  ;
}

The typical JUnit report shows the success or the failures with meaningful messages:

That's it. The following chapters describe the features, typical test scenarios and typical problems when testing PDF documents.