Creating a Java test file

You create a Java file to allow your tests to be automatically discovered and executed. You must create one Java file for each model that you want to test.

Procedure

  1. Open the decision library containing the model that you want to test and create a src/test/java/<packageName> folder.
    The <packageName> can be any name you want.
  2. Create a .java file in the directory you just created, for example MyTest.java.
  3. Add the following content in the Java file:
    package <packageName>;
     
    import com.ibm.decision.run.test.junit5.DecisionTest;
    import com.ibm.decision.run.test.junit5.JSONTestDirectoryFactory;
    import org.junit.jupiter.api.DisplayName;
    import org.junit.jupiter.api.DynamicNode;
    import org.junit.jupiter.api.TestFactory;
     
    import java.util.stream.Stream;
     
    @DecisionTest(decisionFunction = "<functionID"> /* OR  decisionOperation = "<operationID>" */)
    @DisplayName("<DisplayName>")
    public class <TestClassName> {
        @TestFactory
        public Stream<DynamicNode> decisionTests() {
            JSONTestDirectoryFactory.verbose = true;
            return JSONTestDirectoryFactory.createTests(this.getClass());
        }
    }
    Where:
    • <functionID> corresponds to the model name and the function name separated by an hyphen, for example LoanValidation-loan-validation-decision-model.
      Tip: You can use one of the following options to easily find the functionID of the model that you want to test:
      • Build your decision service and open the <model>/target/classes/META-INF/decisions folder. Then, open the JSON file it contains and copy the value of the name property.
      • Run the unit test with an erroneous functionID value. This throws an exception with a list of all the function names that are available.
    • <operationID> corresponds to the operation name of the model.
      Tip: You can use one of the following options to easily find the functionID of the model that you want to test:
      • Build your decision service and open the <model>/target/classes/META-INF/decisions folder. Then, open the JSON file it contains and copy the value of the operationName property.
      • Run the unit test with an erroneous operationID value. This throws an exception with a list of all the operation names that are available.
    • <DisplayName> is the custom name for the test class. This annotation is optional.
    • <TestClassName> begins with Test, for example TestLoanValidation.

    The JSONTestDirectoryFactory.verbose parameter is optional. When set to true, this parameter enables a higher level of verbosity. Running unit tests in verbose mode can be useful when setting up your tests. It returns additional information such as the JSON schema of the input of the decisions.

    The reference documentation for the DecisionTest annotation can be found in the Reference section.

What to do next

You can now define test scenarios in JSON and add them to the src/test/resources folder.