In this tutorial, we will be creating Munits for a simple flow that listens over REST HTTP, send the request to salesforce (via a salesforce connector) and returns a JSON Message in response. The response returned, will be asserted with the expected response.
To build Munits you need to right click API router and select “Create Test Suite for [File Name] from RAML”. This will auto create a basic structure of Munits for you.
The Munits auto created will generally have:
- All the Munits generated for flows that are mentioned in that RAML or WSDL.
- Each Munit flow will have “Set Payload” component that will contain the request message that is needed before starting the flow. This request message is auto picked from RAML if example is defined. MunitTools::getResourceAsString reads the file been specified.
- In Execution section in each Munit flow there will be a flow starter, that will send the request message to a specific flow that needs to be tested out. It can be a flow reference, VM, HTTP “Request” in case of REST service or “Consumer” incase it’s a SOAP service based on the flow to be tested. Since we have build a REST service using RAML; Mule 4 automatically adds an HTTP “Request” component to it. **You might need to configure HTTP Requester or Web Consumer inside Munits so it can call your API’s endpoint.
- In Validation section; Mule 4 auto adds assertions. One checks for the HTTP status code been returned by the API and other on checks for the final response returned by the Mule flow and compared it with the expected response. The expected response is auto picked by Mule 4 if its already defined in RAML inside response example.
You can go ahead and run Munits by right clicking and selection “Run MUnit Suite”.
You can also run Munits from command prompt, just open your command prompt and go to the project root folder and type “mvn test”. This will run you MUnits from command prompt.
Why To Mock Connectors?
If we are not mocking our connectors, on running munits mule 4 will actually connect post request to the external environment through connectors used in out project. In this project since we are using salesforce connector to connect to salesforce environment, on running munits the flow connects to salesforces environment and post its request there. This can be a problem if we want to deploy our application on Production servers; data can get modified even before Mule APIs is deployed successfully.
Thus, mocking all your connectors, ensures that it doesn’t connect to external environment and uses predefined response every time.
How to Mock?
To Mock a connector, we need to place “Mock When” in Behavior section. And define its configuration.
You can set the processor attribute to define the processor to mock with the connector namespace and operation; and the with-attribute element to define the connector’s attribute name and value so that mule can identify which connector is to be mocked. “Then-return” you can define the message that is to be returned by the connector.
With this much configuration we are done with our MUnits.