Testing Business Entity Validation

To test our validation, a test class needs to be created. The one-off generation process has created one for each of the existing entities in the system. The following is the one it created for the Characteristic Type entity:


public class CharacteristicType_Test extends AbstractEntityTestCase {

    private static Logger logger = LoggerFactory.getLogger(CharacteristicType_Test.class);

    /**
      * @see com.splwg.base.api.testers.AbstractEntityTestCase#getChangeHandlerClass()
      */
    protected Class getChangeHandlerClass() {
        return CharacteristicType_CHandler.class;
    }
}

This is a JUnit test case. Let's run it. From within Eclipse, right-click on the test class from within the Package Explorer.

Package Explorer tab in Eclipse Platform. Here a test case is selected, and the options menu shows JUnit Test selected from Run option.

The following image shows the resulting output from JUnit:

JUnit output in Eclipse Platform

As we see, the tests failed and told us that none of our three validation rules where validated. This is, of course true, but some explanation is necessary. When we run entity test cases, the framework looks up the change handler class being tested and collects all of its rules. Then it executes all the tests in the test class (basically every method starting with "test*"). At the end of each test, it looks to see if the last rule violated was one of the rules we are testing. At the end of all the tests, if there are still validation rules that weren't violated, the framework complains. At a minimum, the goal from this point is to create tests that violate each of our rules at least once. Preferably, tests should be created to violate the rules for all additional conditions that we can think of that might compromize the state of the entity.

Let's start fixing our tests with the third rule above the "Foreign Key Reference is required for FK Characteristic Values" rule. With a little head-scratching we determine that this is a "RequireRule" and we replace it as shown below:


public static ValidationRule 
        foreignKeyReferencesRequiredForFkCharValueRule(){ 
    return RequireRule...someFactoryMethod...(
        "CharacteristicType:Foreign Key Reference is required for FK           
             Characteristic Values",
        "If the Characteristic Type Lookup is 'Foreign Key Value' then the
             Foreign Key Reference Cd is required",
      ... some fancy stuff ....
        fkReferencesRequiredForFKCharacteristicValueMessage);
}

Here's the test that was added to the test class to test it:


    /** Test foreignKeyReferencesRequiredForFkCharValueRule  */
    public void testFKReferencesOnlyForFKCharacteristics() {
        // create a new characteristic type
        CharacteristicType charType = createNewTestObject();
        CharacteristicType_DTO charTypeDTO = charType.getDTO();

        // set the characteristic value to null for some other type
        charTypeDTO.setForeignKeyReference("");
        charTypeDTO.setCharacteristicType
              (CharacteristicTypeLookup.PREDEFINEDVALUE);

        // this should be OK
        charType.setDTO(charTypeDTO);

        // Now make it a FK characteristic.  This should violated the rule
        charTypeDTO.setCharacteristicType
              (CharacteristicTypeLookup.FOREIGNKEYVALUE);
        try {
            charType.setDTO(charTypeDTO);
            fail("An error should have been thrown");
        } catch (ApplicationException e) {
            // Make sure the correct rule was violated.
            VerifyViolatedRule
               (CharacteristicType_CHandler. 
               foreignKeyReferencesRequiredForFkCharValueRule());
        }
    }

Note: Important note: Both a valid test AND an invalid test were added to the method above.

Finally, when the test is rerun, we have one validation rule less needing to be violated.

Test run in Eclipse Platform

Iterate Until Done