Ecriture et exécution de tests d'unité

L'étape suivante impliquée dans le test d'un jeu de règles consiste à écrire un test JUnit pour tester la logique de notre jeu de règles. Voici un exemple de test d'unité pour la classe Person. Ce test calcule l'attribut eligible lorsque les attributs isStudent et age sont spécifiés.

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());
  }
}

Ce test peut être ajouté au dossier de test dans le composant que vous utilisez. Vous pouvez exécuter le test comme test JUnit standard via l'environnement de développement intégré que vous utilisez ou également créer une cible Apache Ant pour qu'elle exécute le test pour vous.

Pour plus d'informations sur le test de règles, voir le guide Cúram Express Rules - Manuel de référence.