Testing Microflows with the Unit Testing Module
Introduction
The Unit Testing module provides the capabilities to verify if your microflow logic and your custom Java code works as expected. It provides a user-friendly interface to manage and run unit tests that are created by using microflows, as well as unit tests that are by created using JUnit.
This how-to teaches you how to do the following:
- Set up the Unit Testing module
- Unit test a microflow
Prerequisites
Before starting this how-to, make sure you have completed the following prerequisites:
-
Download Mendix Studio Pro
-
Review the Marketplace components used in this how-to:
Component Version Used in This How-to Unit Testing 9.6.0 All the images, names, and steps in this how-to are based on the Marketplace component versions listed above. When using later versions of this content, images or names on your screen may be different than shown in this how-to.
Setting up the Unit Testing Module
To set up the unit testing module and run the example tests, follow these steps:
-
Create a new app.
-
Download and install the Unit Testing module.
For more information, see How to Use Marketplace Content.
-
In the App Explorer, expand the App {App name} node, and then click Settings.
-
On the Runtime tab of the App settings dialog box, select the After startup microflow.
-
Click Marketplace modules > UnitTesting > _USE ME > Microflows > Startup.
-
Click Select and then OK.
-
In the App Explorer, under the App {App name} node, click Navigation.
-
In the Menu section, click New item.
-
In the Caption field, enter UnitTestOverview.
-
In the On click field, select Call a microflow, and then select the UnitTestOverview microflow.
-
Click Select.
-
Run the app locally.
-
Go to
http://localhost:8080/index.html
. -
In the navigation pane, click UnitTestOverview.
The app shows the UnitTesting page, as in the following image:
You can use this page to execute unit tests, or to reset the test status back to not executed. Use the left-side navigation pane to select a module that contains unit tests. In this scenario, UnitTesting is the only module that contains unit tests.
-
Ensure that the Rollback microflow tests after execution checkbox is selected.
If the checkbox is cleared, all changes made by the microflows that you test are saved to the database. This can result in populating the database with unwanted test data. As a best practice, do not clear the checkbox unless you need to for your specific use case. -
Validate that the Unit Testing module is correctly set up by running the UnitTesting.Test_ValidUnitTest test.
The color of the test case changes to red if the test fails, and to green if it passes.
-
To view detailed test results, click Details in the UnitTesting.Test_ValidUnitTest row.
Unit Testing a Microflow
In this section, you will learn how to create and run a microflow unit test.
Creating a Sample Microflow for Testing
For the purpose of this how-to, create a sample microflow that you can then test with unit testing. In a real-life scenario, the steps below may be different, depending on the microflow that you want to create.
To create a sample microflow for testing, follow these steps:
-
Create a new enumeration with the following properties:
- Name – Level
- Enumeration values Caption and Name – Junior, Medior, and Senior.
-
Open the domain model of MyFirstModule.
-
Create a new entity with the following properties:
- Name – Employee
- Attributes – Name (of the String type) and Level ( of the Enumeration > Level type).
-
Create a microflow called Promote that changes the Level attribute of the Employee entity based on the enumeration value:
- For the Change Employee’s level to Medior activity, set the Level value to MyFirstModule.Level.Medior
- For the Change Employee’s level to Senior activity, set the Level value to MyFirstModule.Level.Senior
Creating a Unit Test
This section describes how to create a microflow-based unit test for the sample microflow described in the Creating a Sample Microflow for Testing section above. In a real-life scenario, the steps below may be different, depending on the microflow that you want to test. For guidelines on creating unit test microflows, see the Creating Unit Tests for Microflows section of Unit Testing.
To create a sample test microflow, follow these steps:
-
In the left-side navigation pane, right-click on MyFirstModule, and then click Add folder.
-
In the Name field, enter UnitTests.
-
Right-click the UnitTests folder, and then click Add microflow.
-
In the Name field, enter Test_PromoteEmployeeToMedior.
-
In the Test_PromoteEmployeeToMedior microflow, add a new Create object activity for the Employee entity.
-
In the Commit section of the activity, select Yes.
-
In the Member section of the activity, add a new member with the following properties:
- Name – ‘John’
- Member type – String (200)
-
In the Member section of the activity, add another new member with the following properties:
- Level – MyFirstModule.Level.Junior
- Member type – Enumeration ‘Level’
-
In the Test_PromoteEmployeeToMedior microflow, call the Promote microflow by adding a Microflow Call activity with the following properties:
- Microflow – MyFirstModule.Promote
- Parameter – Employee
- Argument –$NewEmployee
-
To test if the employee is promoted to the right level, add an Assert using expression activity with the following properties:
- Name – ‘Promoted to Medior?’
- Expression – $NewEmployee/Level = MyFirstModule.Level.Medior
- FailureMessage – ‘Expected employee level to be Medior. Actual value: ’ + getCaption($NewEmployee/Level)
- StopOnFailure - False
The StopOnFailure option determines if the test should stop or continue in case the assertion fails. This gives you the option to still verify other assertions.
A failed assertion will always result in a failed test. -
Right-click the Assert using expression activity that you created.
-
Select Edit caption, and then enter Promoted to Medior? for the new caption.
-
To provide more information about test results for failed tests, add a Report step activity by doing the following steps:
- Add a new Report step activity between Create Employee and Promote
- Set the argument of the Message parameter to string ‘Employee created’.
- Right-click the activity, select Edit caption, and then enter Employee created for the new caption.
- Add a new Report step activity between Promote and Promoted to Medior?.
- Set the argument of the Message parameter to string ‘Employee promoted’.
- Right-click the activity, select Edit caption, and then enter ‘Employee promoted’ for the new caption.
-
Connect the activities, as shown in the following figure:
-
In the left-side navigation pane, in the UnitTests folder, duplicate the Test_PromoteEmployeeToMedior twice, and then rename the new microflows in the following way:
- Test_PromoteEmployeeToSenior
- Test_EmployeeStillSenior
-
In each of the new microflows, make edits to reflect the employee level for that microflow. In the Test_PromoteEmployeeToSenior microflow:
- Set the value of the Level member to MyFirstModule.Level.Medior for the Create Employee activity.
- Set the following values for the Promoted to Medior? activity:
- Name – Promoted to Senior?
- Expression – $NewEmployee/Level = MyFirstModule.Level.Senior
- FailureMessage – ‘Expected employee level to be Senior. Actual value: ’ + getCaption($NewEmployee/Level)
-
In the Test_EmployeeStillSenior microflow:
- Set the value of the Level member to MyFirstModule.Level.Senior for the Create Employee activity.
- Set the following values for the Promoted to Medior? activity:
- Name – Employee still Senior?
- Expression – $NewEmployee/Level = MyFirstModule.Level.Senior
- FailureMessage - ‘Expected employee level to be Senior. Actual value: ’ + getCaption($NewEmployee/Level)
-
Run the app locally and view it.
-
In the left-side navigation pane, click UnitTestOverview > MyFirstModule.
-
Verify that the Rollback microflow tests after execution checkbox is selected.
-
Click Run all module tests and verify that all the test cases pass.