When adding behavior to an entity, it is desirable to do the following:
The following steps are recommended when adding new change handlers so that the additional behavior is sufficiently tested.
PlaceHolderRule
class. Use an appropriate
RuleId
and Description
as self-documentation
of what the rule is supposed to do.AbstractEntityTestCase
. This class should
reference the change handler being added and will ensure 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 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 ensure 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);
}
}