To smarten up your app with business logic you can use microflows. To verify that your microflow works as expected you can create unit tests using the UnitTesting module. The UnitTesting module provides an easy to use interface to manage and run unit tests. The module supports unit tests that are created using microflows and unit tests that are created using JUnit.
After completing this how-to you will know:
- How to set up the UnitTesting module.
- How to create a microflow unit test.
Before you can start with this how-to, make sure you have completed the following prerequisites:
- Download the Mendix Desktop Modeler
Review the software used in this how-to:
Software Version Used in This How-to Mendix Desktop Modeler 5.18.0 UnitTesting module 4.2 Community Commons Function Library module 5.4 ObjectHandling module 1.0
All images, names and steps in this how-to are based on these versions. When using other versions the images and/or names on your screen may be different than the images or names used in this how-to.
2 The UnitTesting Module
In this chapter you will set up the unit testing module and run the example tests.
- Create a new project.
- Download the UnitTesting module.
- Download the Community Commons Function Library module.
- Download the ObjectHandling module.
Open the Settings of the project.
Click the Runtime tab.
Click the Select button to select an after startup microflow.
Select the microflow Startup which can be found in the USE ME folder of the UnitTesting module.
Open the Navigation of the project.
Click New item to add a new item to the menu.
Enter UnitTestOverview in the Caption field.
Select Call a microflow for the On click action, and then select the UnitTestOverview microflow:
Run the project locally.
Click on UnitTestOverview in the navigation.
At the left side 2 buttons are shown. When you click on the Run all button, all unit tests will be executed. When you click on the Reset button, the status of all tests will change to not executed (question mark icon). Beneath the buttons all modules which contains unit tests are shown. In this case UnitTesting is the only module which contains unit tests. When you click on UnitTesting, all unit tests of this module are shown. If you want to save all changes made in the microflow to the database, you need to uncheck the checkbox ‘Rollback microflow tests after execution’. Keep the checkbox checked if you want to rollback all changes. At the right side the unit tests of the selected module are shown. In this example five unit tests are shown. When you click on the Run all button, all unit tests of the selected module will be executed. When you click on the play button next to a unit test, only that unit test will be executed.
Click on the play button of UnitTesting.Test_ValidUnitTest.
The color of the testcase will change to green if it passed.
And to red if the test case fails.
Double click on UnitTesting.TestValidUnitTest.
In this overview you will see the date and time of the last run, the result (success or failed), the latest, reported step and the result message.
3 Creating a Microflow Test
In this chapter you will learn how to create a microflow test. To create a new microflow test in a module, you need to add a microflow with a name that starts with “Test”. A test microflow should have no input arguments and a boolean or string as result type. In case of a boolean, true means success, false means the test failed. In case of a string, any non empty string indicates a failed test.
3.1 Creating a Microflow
Create a new enumeration Level with three enumeration values; Junior, Medior and Senior.
Open the Domain Model of MyFirstModule.
Create an entity Employee with two attributes, Name (String(200)) and Level (Enumeration ‘Level’).
Create a microflow called Promote that looks like this:
3.2 Creating a Unit Test
Add a new folder to MyFirstModule and name it UnitTests.
Add a new microflow to the folder UnitTests and enter Test_PromoteEmployeeToJunior for its name.
The microflow has one input parameter: Employee. So create the Employee object.
Add a new activity of action type Create object.
Select entity Employee.
Set Commit to Yes.
Enter John for the Value of Name.
Now you need to call the Promote microflow with the Employee object you just created. So, add a new activity of action type Microflow call.
Select the Promote microflow.
The next step is to check if the employee is promoted to the right level. Add a new activity of action type Microflow call.
Select theUnitTesting.AssertTrue1 microflow.
Set the argument of parameter ValueToAssert to
$NewEmployee/Level = MyFirstModule.Level.Junior.
Right-click the AssertTrue activity you just configured, select Edit caption, then enter Promoted to Junior? for the new caption.
For failed tests, the last step information can be very useful. You can provide this information in your microflow by calling the ReportStep sub-microflow. So, add a new activity of action type Microflow call between Create Employee and Promote.
Select microflow UnitTesting.ReportStep.
Set the argument of parameter Message to ‘Employee created’.
Add a new activity of action type Microflow call between Promote and Promoted to Junior?.
Select the UnitTesting.ReportStep microflow .
Set the argument of parameter Message to ‘Employee promoted’.
Double-click the End event.
Select Boolean as return type.
Enter true as return value.
The microflow should look like the model below:
Create three more test microflows as shown below:
Run the project locally.
Click on UnitTestOverview in the navigation. MyFirstModule will be shown in the list with modules that contain one or more testcases.
Click on MyFirstModule. All testcases of module MyFirstModule will be shown.
Check the checkbox Rollback microflow tests after execution.
Click on Run all. All test cases should pass.
Congratulations! You created your first unit tests using the UnitTesting module.
It is possible to create a Setup and TearDown microflow per module. The Setup microflow is invoked once before each test run and the TearDown microflow is invoked once after each test run (regardless whether the test run consists of one or multiple unit tests).
Do not test everything, focus on the most used and complex microflows.