1. Vineet Reynolds
  2. Java EE 6-Galleria

Wiki

Clone wiki

Java EE 6-Galleria / TestingTheDomainLayer

Testing the domain layer

The tests in the domain layer consists of :

  • Bean Verification tests
  • Mutual Registration verification tests
  • Tests for the Constructor, equals and hashcode methods
  • Other tests for the entities
  • Tests for the JPA Repositories

Bean Verification tests

Verifying correctness of Java Beans is a chore, but a necessary one. You would want the tests to fail, in a precise manner, if you make an incorrect modification to a getter or a setter. For instance, having a safety net of a test would help you catch bugs faster, like the one below:

public String getPropertyX()
{
    return this.xx //typo - returns xx instead of x
}

public void setPropertyX(String x)
{
    this.x = x;
}

The JPA entities of the application are modelled as JavaBeans in their implementation, and therefore require tests to catch the errors of the above kind. More information on how this is achieved is available in the section on Bean Verification.

Mutual Registration verification tests

The same concerns for bean verification also apply to the Mutual registration pattern employed by the JPA entities. You'll want to verify that modifications to the relationships between the entities, actually result in changes to both the parent and the child properties. More information on this, is available in the section on verifying the Mutual Registration Pattern.

Tests for the Constructor, equals and hashcode methods

Constructors for the domain entities are tested by merely creating new instances of the classes, while asserting the validity of the properties that would be set by the constructor.

The equals and hashcode methods are verified via the use of the EqualsVerifier class from the EqualsVerifier project. If you've read Effective Java (by Joshua Bloch), you'll know the need to verify the correctness of these methods. This is especially true of JPA, where it is necessary to implement these methods to ensure correctness in several operations. The EqualsVerifier project helps you ensure this through a fluent API.

You can read more about implementing the equals and hashcode methods in general, in the Hibernate community wiki.

Other tests for the entities

These are usually one-off tests written for specific cases where the Bean Verification and the Mutual Registration Verification test-models are insufficient or unsuitable. In such instances, the one-off tests verify such behavior through hand-written assertions.

For instance, the mutual registration pattern cannot be verified through the MutualRegistrationVerifier class for the Album.coverPhoto property. The rationale is quite simple - cover photos do not participate in a bidirectional link thereby not implementing the mutual registration pattern. Hence, the behavior of setting cover photos for an Album is verified through several hand-written tests.

Tests for the JPA repositories

The tests for the JPA repositories are covered in a separate section.

Updated