Chapter 9. Testing

9.1. Test Scenarios

Test Scenarios is a powerful feature that provides the ability for developers to validate the functionality of rules, models, and events. In short, Test Scenarios provide you the ability to test your knowledge base before deploying it and putting it into production.

Test Scenarios can be executed one at the time or as a group. The group execution contains all the Scenarios from one package. Test Scenarios are independent, one Scenario can not affect or modify the other.

After running all the Test Scenarios a report panel is shown. It contains either a success message or a failure message for test scenarios that were run.

Figure 9.1. Test Scenario Screen

Test Scenario for the BRMS User Guide 6.0.2

9.2. Creating a Test Scenario

Creating a Test Scenario requires you to provide data for conditions which resemble an instance of your fact or project model. This is matched against a given set of rules and if the expected results are matched against the actual results, the Test Scenario is deemed to have passed.

Creating a new Test Scenario

  1. In Business Central, click AuthoringProject Authoring to open the Projects view.
  2. Select a project for your test scenario.
  3. Click New ItemTest Scenario.
  4. Enter the name, select the package, and click OK.
  5. You will be presented with the Test Scenario edit screen.

    5594

Importing a model for the Test Scenario

Data objects from the same package are available by default. For example, given the package structure org.company.project, and the following:

  • A data object Fact1 in package org.company.
  • A Fact2 in package org.company.project.

If you create your test scenario in org.company, org.company.Fact1 is available but you must import org.company.Fact2. To import data objects:

  1. Open your test scenario.
  2. Click the Data Objects tab.
  3. Click New Item, select your import and click Ok. The imports can be specific to your project’s data model or generic ones like String or Double objects.

Providing Test Scenario Facts

  1. After importing data objects, click the Editor tab. At minimum, there are two sections that require input: GIVEN and EXPECT.

    • GIVEN: The input facts for the test.
    • EXPECT: The expected results given the input facts.

      GIVEN the input parameters, EXPECT these rules to be activated or fired. You can also EXPECT facts to be present and to have specific field values or EXPECT rules not to fire at all.

      If the expectations are met, the test scenario has passed and your rules are correct. Otherwise, the test scenario fails.

Providing Given Facts

  1. To add a new fact, click 5686 next to the GIVEN label. Provide your fact data in the New Input dialog window based on the data models that you have imported in the Data Objects tab.

    6210

    You can select a particular data object from the model and give it a variable name, called Fact Name in the window, or choose to activate a rule flow group instead. Activating a rule flow group allows rules from the specified rule flow group to be tested by activating the group in advance. To add a given fact and activate a rule flow group:

    1. Add the given fact.
    2. Click 5686 again and add the rule flow group activation.
  2. Optionally, add restrictions on the object you will insert.

    1. Click Add a field and select a property of your object.

      5606
    2. Click 6191 next to the property.

    3. Provide the value. For example:

      5607

      The example above is equivalent to the following:

      Customer fact1 = new Customer();
      fact1.setHasInternetService(true);
      insert(fact1);

Providing Expected Rules

  1. Once you are satisfied with the GIVEN conditions, you can expect rules that will be fired, facts created, or field values in existing facts changed. Click 5686 next to the EXPECT label to start adding expected results.

    5611

  2. You can provide one of three expectations given the set of data that was created in the Given section:

    • Rule: enables you to check for firing of a particular rule. Either type the name of a rule that is expected to be fired or select it from the list of rules. Click the OK when done.
    • Fact value: enables you to check a specific object instance and its values. In the following example, given a Customer object with the hasInternetService boolean set to true, we expect the same object to have the hasPhoneService boolean set to true:

      factvalues
    • Any fact that matches: enables you to check any objects in the working memory and the values of their field. In the following example, given a Customer object which has internet service, a new object RecurringPayment is expected to be inserted into the working memory with the amount field set to 5:

      5612

Reviewing, Saving, and Running a Scenario

  1. Click Save in the upper right corner to save your scenario. Ensure you regularly save and review your scenarios.
  2. Click Run scenario in the upper right corner to execute your test. The results are displayed at the bottom of this screen in a new panel called Reporting.

    5613
  3. If you created more tests in one file, you can run all the tests in a sequence. Click Run all scenarios to do so.
  4. Also note the Audit log:, which informs you about inserted facts and fired rules:

    auditlog

9.3. Additional Test Scenario Features

In addition to the previous Test Scenario features, Test Scenarios include various other features.

Calling Methods

  1. Call Method enables you to call a method on an existing fact in the beginning of the rule execution. This feature is accessed by clicking 6176 next to the CALL METHOD label.

    Figure 9.2. Call Method

    6175
  2. After selecting an existing fact from the drop-down list, click Add. The green arrow button 6187 enables you to call a method on the fact.

    Figure 9.3. Invoke a Method

    6188

Using Globals in a Test Scenario

Globals are named objects that are visible to the rule engine but are different from the objects for facts. Accordingly, the changes in the object of a global do not trigger the reevaluation of rules. You can use and validate global fields in a Test Scenario.

To make a global variable accessible for your test scenario:

  1. Click New ItemGlobal Variable(s) to create a global definition.
  2. Define your global variable name and type.
  3. Import the object type in your test. If you do not import the type of your global variable, the variable will not be accessible for your test.

Adding a New Global

  1. Click 6176 next to the (globals) label to add a global and click Add.

    New Global pop up dialog for JBoss BRMS Test Scenario feature.

Adding restrictions on fields is similar to adding fields and restrictions in the Given section. See Providing Given Facts for further information.

Configuring Rules

  1. The (configuration) label enables the you to set additional constraints on the firing of rules by providing the following options:

    • Allow these rules to fire: enables you to select which rules are allowed to fire.
    • Prevent these rules from firing: enables you to prevent certain rules from firing for the test scenario.
    • All rules may fire allows all the rules to fire for the given test.

    Figure 9.4. Configuration

    Configuration rules option for JBoss BRMS Test Scenario features
  2. If you select one of the following:

    • Allow these rules to fire:
    • Prevent these rules from firing:

      Enter the rules into the empty field. Clicking 6176 next to the empty field to select which rules are affected by the condition.

      Figure 9.5. Selecting rules

      Configuration select rule options for the JBoss BRMS Test Scenario feature.
  3. Choose a rule from the drop-down list and click OK. The selected rules will appear in the field next to the rules configuration option.

Date and Time Configuration

  1. The Use real date and time option uses real time when running the test scenario.

    Figure 9.6. Real Date and Time

    Use real date and time choice for the Expect feature in JBoss BRMS Test Scenarios
  2. The Use a simulated date and time option enables you to specify the year, month, day, hour, and minute associated with the test scenario.

    Figure 9.7. Title

    Simulated date and time for Test Scenarios in JBoss BRMS 6.0.2

Advanced Fact Data

  1. After providing fields to editable properties as part of your created fact, click 6191 to open the Field value dialogue. You can edit literal values or provide advanced fact data.

    Figure 9.8. Advanced Options

    Advanced Fact Data options for JBoss BRMS Test Scenarios.
  2. In the Advanced Options…​ section, you can choose between the following, depending on the type of fact created and the model objects used for the particular test scenario.

    • Bound variable sets the value of the field to the fact bound to the selected variable. The field type must match the bound variable type.
    • Create new fact enables you to create a new fact and assign it as a field value of the parent fact. Click on the fact to be assigned as a field value to be supplied with a drop down of various field values. These values may be given further field values.

Adding More Sections

  • The Editor tab enables you to add GIVEN, CCALL METHOD, and EXPECT sections to the scenario. Click More below the EXPECT section to do so. This will open a block with all three sections that can be removed by clicking 6209 .

Modifying or Deleting an Existing Fact

When you create more tests in one file, it is recommended to delete facts inserted by previous tests. When you insert a new GIVEN fact, notice the following fields:

  • Modify an existing fact enables you to edit a fact between knowledge base executions.
  • Delete an existing fact enables you to remove facts between executions.

    Figure 9.9. Modifying and Deleting Existing Facts

    Modify and Delete and existing fact options for JBoss BRMS Test Scenarios