Develop Configurable Parameterized MUnits (APIKit) in Mule 4
This article will explain the steps to configure and validate the designed API resource in Mule 4.
Join the DZone community and get the full member experience.
Join For FreeThis article will explain the steps to configure and validate the designed API resource in Mule 4.
APIKit is a critical part of API resource behavior while exposing to an external system hence unit test cases should be executed with maximum error codes supported by HTTP verbs.
Step 1
Follow MuleSoft API LifeCycle and design API resources in RAML, import it to Anypoint Studio.
Step 2
Develop MUnit test cases along with configured parameterized input file. This parameterized file will produce as input for events triggered from MUnit flow. Flow steps to develop an MUnit test case for the scenario.
2.1 Configured input parameterized file as below
2.2 Parameterized file will look like the given snippet:
BAD_REQUEST:
errorType: "APIKIT:BAD_REQUEST"
statusCode: "400"
httpStatus: "400"
message: ""
NOT_FOUND:
errorType: "APIKIT:NOT_FOUND"
statusCode: "404"
message: "Not Found Error"
httpStatus : "404"
METHOD_NOT_ALLOWED:
errorType: "APIKIT:METHOD_NOT_ALLOWED"
statusCode: "405"
message: "Method Not Allowed Error"
httpStatus : "405"
NOT_ACCEPTABLE:
errorType: "APIKIT:NOT_ACCEPTABLE"
statusCode: "406"
message: "Not Acceptable Error"
httpStatus : "406"
UNSUPPORTED_MEDIA_TYPE:
errorType: "APIKIT:UNSUPPORTED_MEDIA_TYPE"
statusCode: "415"
message: "Unsupported MediaType Error"
httpStatus : "415"
EXPRESSION:
errorType: "EXPRESSION"
statusCode: "500"
message: "Expression Error"
httpStatus: "500"
STREAM_MAXIMUM_SIZE_EXCEEDED:
errorType: "STREAM_MAXIMUM_SIZE_EXCEEDED"
statusCode: "500"
message: "Stream Maximum Size Exceeded Error"
httpStatus: "500"
NOT_IMPLEMENTED:
errorType: "APIKIT:NOT_IMPLEMENTED"
statusCode: "501"
message: "Not Implemented"
httpStatus: "501"
2.3 Steps in MUnits
- Flow-ref: configure API main flow.
- Assign message and status code to variable to reuse in assert component.
variable: munitStatusCode
variable: munitStatusCode
- Configure Assert expression, MuleSoft integration flow points exception as below assumed in assert component.
import * from dw::test::Asserts
---
payload must equalTo (
{
"code": vars.munitStatusCode,
"message": vars.munitMessage
}
)
- Below will be the final flow of MUnit to evaluate configured input in the parameterized file:
- Run the MUnit test cases to assess outcomes with scenarios...
Useful link: Parameterized
Opinions expressed by DZone contributors are their own.
Comments