I will create a generic test suite using parameterisation in MuleSoft MUnits to test different test scenarios (e.g.: Positive Test, Negative Test/Specific Error scenarios).
Test cases will be reusable using parameterised test suites, which run the same test cases across APIs (across different resources/endpoints) with different inputs.
The below steps will help generate these reusable test cases.
Once API development is completed, start by creating a blank MUnit test. Add MUnit Config to include the parameterised file. For example, the system API name (sfdc-sapi). All these test cases are related to happy/positive test suites.
File Name: common/parameterizations-positive.yaml
You can define all parameters which are required for the test case. For example, Flow Name, Input Payload, Input Attributes and Process name need to be Mocked, Mock Attribute Name, Mock Processor Name, Output from Mock Processor, Out Payload from Calling Flow, Error Type and HTTP Status. You can add/delete as per your requirements.
Test Suite: salesforce-system-api-positive-test
salesforce-system-api-positive-test
Details about each component:
Add the Munit tools as required by your test flows and externalise each value with a dollar sign and curly brackets ${mockParam}.
Mock Processor with Attribute name and value:
- Provide Payload
- Provide Attribute (if needed)
Set Event
- Set Input Payload
- Set Input Params (Query Params and Header Params)
Name of the Flow which needs to be tested.
Set Output Payload Variable (Out Payload/Response Payload value from current flow). This is an optional step. You can directly Assert Equals as mentioned in the below step.
Assert Equals to Validate Out Response
Similarly, you can add test suites for implementation as well.
All these test cases are related to unhappy/negative test suites.
File Name: common/parameterizations-negative.yaml
You can define all parameters which are required for the test case. For example, Flow Name, Input Payload, Input Attributes and Process name need to be Mocked, Mock Attribute Name, Mock Processor Name, Output from Mock Processor, Out Payload from Calling Flow, Error Type and HTTP Status. You can add/delete as per your requirements. Here, we are going to test HTTP:BAD_REQUEST scenario.
Test Suite: salesforce-system-api-negative-test
salesforce-system-api-negative-test
Details about each component:
Add the MUnit tools as required by your test flows and externalise each value with a dollar and curly brackets ${mockParam}
Mock Processor with Attribute name and value
- Provide Payload
- Provide Attribute (if needed)
Set Event
- Set Input Payload
- Set Input Params (Query Params and Header Params)
Name of Flow which needs to be tested
Assert Equals to Validate HTTP Status Code (user can also validate out payload as well if needed and/or required).
Similarly, you can add test suites for implementation as well.
Benefits of implementing these MUnit Test suites:
- Limited knowledge will be required to write MUnits.
- New Scenarios/Test Cases can be easily added by just creating in/out Payload and in/out Attributes without modification of existing test suites.
- Easily incorporate multiple test cases for Positive/Negative Test cases (for example, Experience API, System API), EAPI/SAPI will have direct 1-on-1 calls, whereas Process API (PAPI) will have multiple System API calls which require multiple mock-when processors (Note: Refer to the Multiple Mock Scenarios section.)
- MUnit Coverage can be easily increased or achieved ~100%.
Limitation:
The above test cases can handle only 1 mock-when (but you can add For-Each and customised mock-when as per your use case).
Test Results: (postOrders)
Positive Test Suites
Negative Test Suites
Note: Here, you see the test result as 75% (in the negative test case) because post processors after HTTP Request were not executed, and it is the natural behaviour of the test case.
Run both Test Suites (Positive and Negative) at the same time.
Note: Here, you see the test result as 100% because both test cases were executed successfully.
Multiple Mock Scenarios
- YAML File for all Test Suites
- Sample Test Suite scenario
- Use can set error scenarios as I did for Variables and Payload.