Writing and Running Unit Tests

The next step involved in testing a rule set is to write a JUnit test to test the logic in our rule set. Below is an example of a unit test for the Person class. This test calculates the eligible attribute when the isStudent and age attributes are specified.

import curam.creole.calculator.CREOLETestHelper;
import curam.creole.execution.session.RecalculationsProhibited;
import curam.creole.execution.session.Session;
import curam.creole.execution.session.Session_Factory;
import curam.creole.execution.session.StronglyTypedRuleObjectFactory;
import curam.creole.ruleclass.SampleBenefit.impl.Person;
import curam.creole.ruleclass.SampleBenefit.impl.Person_Factory;
import curam.creole.storage.inmemory.InMemoryDataStorage;
import curam.test.framework.CuramServerTest;


/** Class tests the Person rule class.*/
public class TestPersonEligible extends CuramServerTest {

  private Session session;


  public TestPersonEligible(String arg0) {
    super(arg0);
  }


  /*
   * All tests in this class will use a newly-created session 
   * that creates strongly-typed rule objects
   */
  @Override
  protected void setUpCuramServerTest() {
    session = Session_Factory.getFactory().newInstance(
      new RecalculationsProhibited(),
        new InMemoryDataStorage(
          new StronglyTypedRuleObjectFactory()));
  }


  /**
   * Tests that a person's eligibility is correctly calculated 
   * when supplied a person's student status and age.
   */
  public void testPersonEligiblity() {

    /* Create a "bootstrap" rule object */
    final Person personObj = 
      Person_Factory.getFactory().newInstance(session);

   /**
    * Specify a person with an age of 25.
    */
    personObj.age().specifyValue(25);
   
    /**
     * Specify a person who is a student.
     */
    personObj.isStudent().specifyValue(true);
    
    /**
     * Check that the person is eligible
     */
    CREOLETestHelper.assertEquals(
      true, personObj.eligible().getValue());
  }
}

This test can be added to the test folder in the component you are working on. You can run the test as a standard JUnit test through the Integrated Development Environment you are using or alternatively you can create an Apache Ant target to run the test for you.

For more information on testing rules see the guide Cúram Express Rules Reference Manual.