Testing the Change Handler Class

When adding behavior to an entity, it is desirable to do the following:

  • Break the rules into modular pieces that can be independently maintained and tested.
  • Test that each behavior works by creating a JUnit test for each distinct behavior.

The following steps are recommended when adding new change handlers so that the additional behavior is sufficiently tested.

  • Add each rule to the change handler at once using instances of the PlaceHolderRule class. Use an appropriate RuleId and Description as self-documentation of what the rule is supposed to do.
  • Add a new test class by extending AbstractEntityTestCase. This class should reference the change handler being added and will insure that each rule is violated by at least one test. The test class name should end with "Test".
  • Run the test class as a JUnit test. The test class should complain that there was at least one rule that was not violated by the test class. For the rule that was not violated, add a test method to the test class and also add the "real" validation logic to the change handler class. Try executing the test class again. Continue implementing more test methods and rules until all rules are tested and the JUnit class completes successfully. Below is an example, test method for a rule that tests both a successful change and an unsuccessful change. It is important to insure that the validation error is thrown by the actual rule being tested.

    
    public void testAddressOneLabelRequiredIfAddressOneIsAvailable() {
       //pass
       Country country = (Country) createQuery(
           "from Country country").firstRow();
       Country_DTO countryDto = country.getDTO();
       country.setDTO(countryDto);
    
       //fail
       countryDto.setAddress1Available(Bool.TRUE);
       countryDto.setLanguageAddress1("");
       try {
           country.setDTO(countryDto);
           fail("A validation error should have been thrown");
       } catch (ApplicationException e) {
         verifyViolatedRule(Country_Chandler
             .addressOneLabelRequiredIfAddressOneIsAvailable(), e);
       }
       }
    
    
  • Add other test methods to test "handle" methods on the change handler as well as business methods that may have been added.