4.7.  Comparing Form Fields

Quantity

The first and simplest test is to check that a document has the same number of form fields as the referenced document:

@Test
public void haveSameNumberOfFields() throws Exception {
  String filenameTest = "documentUnderTest.pdf";
  String filenameReference = "reference.pdf";
  
  AssertThat.document(filenameTest)
            .and(filenameReference)
            .haveSameNumberOfFields()
  ;
}

Field Names

The next test checks that the number of fields and their names are equal in both PDF documents:

@Test
public void haveSameFields_ByName() throws Exception {
  String filenameTest = "documentUnderTest.pdf";
  String filenameReference = "reference.pdf";
  
  AssertThat.document(filenameTest)
            .and(filenameReference)
            .haveSameFieldsByName()
  ;
}

Field Content

And finally, you can compare the contents in form fields in two documents using the method haveSameFieldsByValue(). Fields with the same name must have the same contents:

@Test
public void haveSameFields_ByValue() throws Exception {
  String filenameTest = "documentUnderTest.pdf";
  String filenameReference = "reference.pdf";
  
  AssertThat.document(filenameTest)
            .and(filenameReference) 
            .haveSameFieldsByValue() 1
  ;
}

1

Whitespaces are normalized, see chapter 13.5: “Whitespace Processing”.

Concatenated Tests

Multiple and different test methods can be concatenated in one test, but such a test is not recommended because it's hard to find a good name:

@Test
public void compareManyItems() throws Exception {
  String filenameTest = "documentUnderTest.pdf";
  String filenameReference = "reference.pdf";
  
  AssertThat.document(filenameTest)
            .and(filenameReference)
            .haveSameAppearance()
            .haveSameText
  ;
}