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.Date;
    import java.util.logging.*;
    import java.util.stream.Stream;
    
    @DecisionTest(decisionOperation = "<operationID>")
    @DisplayName("<DisplayName>")
    public class <TestClassName> {
        @TestFactory
        public Stream<DynamicNode> decisionTests() {
            Logger logger = Logger.getLogger("com.ibm.decision.run.test");
            ConsoleHandler handler = new ConsoleHandler();
            handler.setLevel(Level.ALL);
            logger.addHandler(handler);
            logger.setLevel(Level.FINEST);
    
            return JSONTestDirectoryFactory.createTests(this.getClass());
        }
    }
    
    Where:
    • <operationID> corresponds to the model name and the function name separated by an hyphen, for example LoanValidation-loan-validation-decision-model.
      Tip: To easily find the operationID 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.
    • <DisplayName> is the custom name for the test class.
    • <TestClassName> begins with Test, for example TestLoanValidation.

What to do next

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