Prev |  | Next

## 3.23. Page Numbers as Objectives

### Overview

It is sometimes useful to check if a generated PDF document has exactly one page. Or maybe you want to ensure that a document has less than 6 pages, because otherwise you have to pay higher postage. PDFUnit provides suitable test methods:

    // Method for tests with pages:
    .hasNumberOfPages(..)
    .hasLessPagesThan(..)
    .hasMorePagesThan(..)

### Examples

You can check the number of pages like this:

    @Test
    public void hasNumberOfPages() throws Exception {
      String filename = "documentUnderTest.pdf";

      AssertThat.document(filename)
                .hasNumberOfPages(1)
      ;
    }

Tests are also possible with a minimum or maximum number of pages.

    @Test
    public void hasLessPagesThan() throws Exception {
      String filename = "documentUnderTest.pdf";
      int upperLimitExclusive = 6; // The document has 5 pages

      AssertThat.document(filename)
                .hasLessPagesThan(upperLimitExclusive)
      ;
    }

    @Test
    public void hasMorePagesThan() throws Exception {
      String filename = "documentUnderTest.pdf";
      int lowerLimitExclusive = 2; // The document has 5 pages

      AssertThat.document(filename)
                .hasMorePagesThan(lowerLimitExclusive)
      ;
    }

| The values for upper- and lower limits are exclusive.

Of course methods can be concatenated:

    @Test
    public void hasNumberOfPages_InRange() throws Exception {
      String filename = "documentUnderTest.pdf";
      // The current document has 5 pages
      int lowerLimit_2 = 2;  // the limit is exclusive
      int upperLimit_8 = 8;  // the limit is exclusive

      AssertThat.document(filename)
                .hasMorePagesThan(lowerLimit_2)
                .hasLessPagesThan(upperLimit_8)
      ;
    }

Don't omit tests with page numbers just because you might think they are too simple. Experience shows that you can find errors in the context of a primitive test that you would not have found without the test.

Prev | Up | Next
3.22. Number of PDF Elements | Home | 3.24. Passwords