Testing microflows using the UnitTesting module

5 minutes to read Download PDF Edit

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.

1. Preparation

Before you can start with this how-to, make sure you have completed the following prerequisites.

  • Download the Mendix Modeler.

SoftwareVersion used in this how-to
Mendix Modeler5.18.0
UnitTesting module4.2
Community Commons Function Library module5.4

2. The UnitTesting module

In this chapter you will set up the unit testing module and run the example tests.

  1. Create a new project.
  2. Download the UnitTesting module.
  3. Download the Community Commons Function Library module.
  4. Open the Settings of the project.
  5. Click tab Model.
  6. Click the Select button to select an after startup microflow.
  7. Select the microflow Startup which can be found in the _USE ME folder of the UnitTesting module.
  8. Click OK.
  9. Open the Navigation of the project.
  10. Click New item to add a new item to the menu.
  11. Enter UnitTestOverview in the Caption field.
  12. Select the microflow UnitTestOverview as target.
  13. Click OK.
  14. Run the project locally.
  15. Go to http://localhost:8080/index.html.
  16. 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. 

  17. 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.

  18. 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. Create 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 Create a microflow

  1. Create a new enumeration Level with three enumeration values; Junior, Medior and Senior.
  2. Open the Domain Model of MyFirstModule.
  3. Create an entity Employee with two attributes, Name (String(200)) and Level (Enumeration ‘Level’).
  4. Create a microflow Promote.

3.2 Create a unit test

  1. Add a new folder to MyFirstModule and name it UnitTests.

  2. Add a new microflow to the folder UnitTests and name it _TestPromoteEmployeeToJunior.

    The microflow Promote has one input parameter, Employee. So first you need to create the object Employee. 

  3. Add a new activity of action type Create object.

  4. Select entity Employee.

  5. Set Commit to Yes.

  6. Set Name to John.

  7. Click OK.

    Now you need to call the microflow Promote with the just created object Employee. 

  8. Add a new activity of action type Microflow call.

  9. Select microflow MyFirstModule.Promote.

  10. Click OK. The next step is to check if the employee is promoted to the right level. 

  11. Add a new activity of action type Microflow call.

  12. Select microflow UnitTesting.AssertTrue1.

  13. Set the argument of parameter ValueToAssert to $NewEmployee/Level = MyFirstModule.Level.Junior.

  14. Click OK. For failed tests, the last step information can be very useful. You can provide this information in your microflow by calling the ReportStep submicroflow.

  15. Add a new activity of action type Microflow call between Create Employee and Promote.

  16. Select microflow UnitTesting.ReportStep.

  17. Set the argument of parameter Message to ‘Employee created’.

  18. Click OK.

  19. Add a new activity of action type Microflow call between _Promote_ and Promoted to Junior?.

  20. Select microflow UnitTesting.ReportStep.

  21. Set the argument of parameter Message to ‘Employee promoted’.

  22. Click OK.

  23. Double-click on the End event.

  24. Select Boolean as return type.

  25. Enter true as return value.

  26. Click OK.

  27. The microflow should look like the model below:

  28. Create three more test microflows as shown below:

    • Test_PromoteEmployeeToMedior

    • Test_PromoteEmployeeToSenior

    • Test_PromoteEmployeeWhenAlreadySenior

  29. Run the project locally.

  30. Go to http://localhost:8080/index.html.

  31. Click on UnitTestOverview in the navigation.

    MyFirstModule will be shown in the list with modules that contain one or more testcases.

  32. Click on MyFirstModule.

    All testcases of module MyFirstModule will be shown.

  33. Check the checkbox Rollback microflow tests after execution.

  34. Click on Run all. All testcases should pass

Congratulations! You created your first unit tests using the UnitTesting module.

Do not test everything, focus on the most used and complex microflows.