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
-
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. - Create a
.javafile in the directory you just created, for exampleMyTest.java. - 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 thefunctionIDof 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
nameproperty. - Run the unit test with an erroneous
functionIDvalue. This throws an exception with a list of all the function names that are available.
- 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
- <operationID> corresponds to the operation name of the model.Tip: You can use one of the following options to easily find the
functionIDof 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
operationNameproperty. - Run the unit test with an erroneous
operationIDvalue. This throws an exception with a list of all the operation names that are available.
- 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
- <DisplayName> is the custom name for the test class. This annotation is optional.
- <TestClassName> begins with
Test, for exampleTestLoanValidation.
The
JSONTestDirectoryFactory.verboseparameter is optional. When set totrue, 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
DecisionTestannotation can be found in the Reference section. - <functionID> corresponds to the model name and the function name separated
by an hyphen, for example
What to do next
You can now define test scenarios in JSON and add them to the src/test/resources
folder.