Create a Unit Test Suite¶ Once you have developed an integration solution, WSO2 Integrator: MI VS Code Extension allows you to build unit tests for the following: Test mediation sequences and REST apis with multiple test cases. Test the artifacts with registry resources. Test the artifacts with Connectors. Note Scheduled triggers are not supported by the Unit Testing framework. Create Unit Test Suite¶ Download and install the WSO2 Integrator: MI server and on your computer. Launch Visual Studio Code with the WSO2 Integrator: MI Extension installed. Select the Testing extension from the left side panel to view the Test Explorer. Click on Add New Test Suite in the Test Explorer. Specify a name for the unit test suite. Then, select the artifact type that you want to test in order to get the list of artifacts for the selected type. Note You can only select one sequence or API artifact per unit test suite. Select the artifact to which you want to write the test cases. Note You can also use AI to generate a Unit Test Suite for the selected artifact. To add a new test case to the test suite, click on Add test case. Enter the following information: Enter a name for the test case. Note For APIs, you also need to specify the Resource Path and Resource Method in this section. The Resource Path indicates the URL mapping of the API resource. If the URL mapping consists some parameter(s), replace those with values. Also the Resource Method indicates the REST method of the resource. Update the Input Payload and Properties section: Input Payload: The input payload of the test case. This can be JSON, XML, or plain text. Input properties: The input properties of the test case. There are three types of properties allowed in unit testing: Synapse(ctx), Axis2(axis2), and Transport($trp) properties. Note For sequences, the test suite allows to add all type of properties with the value. But for APIs you are only allowed to add transport properties. In the Assertions section, you can add multiple assertions belonging to two types. AssertEquals to check the whether the mediated result and expected values are equal. AssertNotNull to check whether the mediated result is not null. Fill the following information for each assertion. Assertion Type: Type of the assertion. Assertion: Item that you want to assert. Payload: assert the payload Status Code: assert status code of the service Transport Header: assert a transport property Note When Assertion is set to Transport Header, an additional field called Transport Header will be displayed to enter the header name HTTP Version: assert http version of the service Expected Value: Expected value for the assertion (only when Assertion Type is set to Assert Equals). Type can be a JSON, XML or a plain text. Error Message: Error message to print when the assertion is failed. Once you have added the properties and assertions, click on Create to save the unit test case. To add a mock service to the test suite, click on Add mock service. You can use a mock service to simulate the actual endpoint. If you have an already created mock service, select the mock service from the list existing mock services as shown below or create a new mock service. Once the required information is completed, click on Create. Create Mock Service¶ To create a new Mock Service, click Add New. Enter the following information: Name of the Mock Service: A name for the mock service. Mocking Endpoint Name: Endpoint name which wants to mock. Mock Service Port: Port for the mock service. Mock Service Context: Main context for the service starts with '/'. Add multiple resources for the mock service as needed. To add multiple resources click the Add mock service resource button. Enter the following information for the mock resource: Service Sub Context: Sub context of the resource starts with '/'. Service Method: REST method type of the resource. Fill the Expected request to resource section if you want to mock an endpoint based on the coming request headers or payload. Header Name: Expected request header name. Header Value: Expected request header value. Expected Request Payload: Expected request payload to the service. Info Entered request headers/payload must be matched with the request to send the response this mock service. Fill the Expected response from resource section to get a response from the service. Response Status Code: Response status code of the mock service. Header Name: Response request header name. Header Value: Response request header value. Expected Response Payload: Expected response payload from the service. Once the required information is completed in the Mock Resource Configuration page, click on Submit. Review the information given and click on Create to complete the mock service creation. Info Please note that the mock service should have a sub context with '/' defined, and additional sub contexts should be defined after that. If you are trying to mock an endpoint http://petstore.com/pets where the mock service context is /pet, the sub-context should be /. If you are trying to mock an endpoint http://petstore.com/pets/id where the mock service context is /pet, the sub-context should be /id. Update Unit Test Suite¶ Switch to the Testing extension from the left side panel of the VS Code to view the Test Explorer in which the existing test suites will be listed. Click on the Diagram icon which is on the right side of any given unit test suite in order to update it by following the steps similar to its creation process. Run Unit Test Suite¶ Switch to the Testing extension from the left side panel of the VS Code to view the Test Explorer in which the existing test suites will be listed. Click on the Run icon which is on the right side of any given unit test suite in order to run it and get the test results. Note Additionally, the tests automatically run when executing mvn clean install. If you want to run unit tests with your configured server, follow the steps: Run a separate WSO2 Integrator: MI Instance in unit testing mode. To start the server in unit testing mode, you can pass the argument -DsynapseTest as below. If you want to change the synapse testing port, you can pass the -DsynapseTestPort=<new Port> argument. Default port is 9008. It is recommended running on a new MI server without any previously deployed integrations to avoid conflicts. sh micro-integrator.sh -DsynapseTest Navigate to the integration project location and run the following command. mvn clean install -DtestServerType=remote -DtestServerHost=localhost -DtestServerPort=9008 -P test View unit test coverage¶ After running unit tests, the framework automatically calculates and displays line coverage based on the mediators executed during test execution. The coverage percentage indicates the proportion of mediators in your integration artifacts (APIs, sequences, templates) that were executed at least once during the test run. When you run unit tests, a coverage summary is displayed in the console output showing: Test Suite Coverage: Overall coverage percentage for the main artifact being tested Supportive Artifact Coverage: Individual coverage percentages for any supportive artifacts used (sequences, templates, registry resources) For a detailed coverage report, review the JSON report generated at <project-name>/target/unit-test-report.json. This detailed report provides granular information about which specific mediators were covered and which were not, helping you identify gaps in your test coverage. Tip Aim for high coverage percentages to ensure your integration logic is thoroughly tested. If you notice low coverage for certain artifacts, consider adding additional test cases to exercise untested mediators. Generate unit test suite using AI¶ In the Create New Unit Test page, give a test suite name, select the artifact type and the artifact to be used for the test suite. Click Generate Unit Tests with AI and wait until the AI service responds with the generated test suite and test cases. Note AI services require a signed-in WSO2 Integrator Copilot account. You will be prompted to sign in if you haven't already. Generate Unit Test Case using AI¶ Note Adding a test case using AI, is only available for test suites that have already been created. In the Update Unit Test page, click on Add Test Case. Click on Generate Test Case with AI. Input fields are not required to be filled. Fill the Test Case Description explaining the test case that needs to be added. Click on Generate. Wait for the AI service to respond with the generated test case. Note The newly generated test will be added to the existing test suite as a new test case.