DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • Error Handling Inside Kumologica Subflow
  • Testing Jedis API Using Junit Test Case in Eclipse IDE
  • How to Build an API Testing Program with MuleSoft Anypoint
  • Mocking and Its Importance in Integration and E2E Testing

Trending

  • Exploring Intercooler.js: Simplify AJAX With HTML Attributes
  • Can You Run a MariaDB Cluster on a $150 Kubernetes Lab? I Gave It a Shot
  • Simplifying Multi-LLM Integration With KubeMQ
  • Intro to RAG: Foundations of Retrieval Augmented Generation, Part 1
  1. DZone
  2. Testing, Deployment, and Maintenance
  3. Testing, Tools, and Frameworks
  4. Mock API KIT: Router and Write Munit Test Cases for Different Error Types

Mock API KIT: Router and Write Munit Test Cases for Different Error Types

This article is a tutorial for writing Munit Test cases in order to meet the required coverage threshold for different error types.

By 
Abhishek Bathwal user avatar
Abhishek Bathwal
·
Jan. 16, 21 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
16.8K Views

Join the DZone community and get the full member experience.

Join For Free

Whenever we write Munit Test cases for any flow, coverage is one of the very important components which we have to take care of. But, due to error handling, it's become difficult to get the required coverage.

But, to meet the coverage threshold we have two options:

  1. Ignore the Error Handling flow.
  2. Write test cases for Error handling.

Note: The blog will have a detailed descriptive solution for writing the test cases for Error Handling. 

Ignoring the Error Handling flow:

For ignoring a flow, we can use the ignore plugins in the POM. So, when we execute the Munit, the particular flow will be ignored and we get a better coverage report.

<plugins>

  <plugin>

    <groupId>com.mulesoft.munit.tools</groupId>

    <artifactId>munit-maven-plugin</artifactId>

    <configuration>

      ...

      <coverage>

        <ignoreFlows>

             <ignoreFlow>flow-name</ignoreFlow>

        </ignoreFlows>

     </coverage>

      ...

    </configuration>

  </plugin>

</plugins>


But, the Munit Studio has a limitation with this. The Munit Coverage report does not reflect this in the report. To get the exact coverage percentage, please follow the report from the Console.

Or, Execute the test cases from the Command prompt.

Script for cmd execution:

mvn test or mvn clean test or mvn test -X

Writing test cases for Error handling:

We have a simple flow with one resource and one error handling block as API KIT:BAD_REQUEST.

The Transform Message of the AKIKIT:BAD_REQUEST contains a payload with a message and Variable with HTTP status. (We can have any custom message in the payload as per requirement).

Now, we will write a test case for the main flow, which contains the error propagate block.

It Contains a Mock component as Behavior, a Flow reference as Execution step, and an Assert Equal component in Validation.

The Flow Reference will refer to the main flow below:

Next, we are going to configure the mock where we will Mock the API KIT router and define the Error Typeid in the Error tab. Also, in the suite flow configuration, we need to define the Expected error type. These two steps are very important as on the basis of this only the test case for error propagate will work.

Mock Component: 

Flow Configuration:

For Assert That, the assertion can be on the basis of payload or httpStatus code. It completely depends on the user. For our application, we selected HTTP status for validating.

As all the configurations are done and we have set up the test flow, we will execute the test case.

Console Result:

= Tests run: 1 - Failed: 0 - Errors: 0 - Skipped: 0 - Time elapsed: 0.18 sec


Munit Studio Result:

Munit Coverage Report:

Note: As we have not written any test case for the post:\demo resource flow and only for Main flow, so the coverage for demo resource flow is 0% and for Main Flow is 100%.

Once the test case is executed, the part which comes under the success test execution is marked with a green check. This indicates that the test cases for those components are done successfully. 

In a similar way, we can write test cases for other Error Types as well.

Sample Munit Code:

<?xml version="1.0" encoding="UTF-8"?>

<mule

    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"

    xmlns:munit="http://www.mulesoft.org/schema/mule/munit"

    xmlns:munit-tools="http://www.mulesoft.org/schema/mule/munit-tools"

    xmlns="http://www.mulesoft.org/schema/mule/core"

    xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"

    xsi:schemaLocation="

        http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd

        http://www.mulesoft.org/schema/mule/munit http://www.mulesoft.org/schema/mule/munit/current/mule-munit.xsd

        http://www.mulesoft.org/schema/mule/munit-tools  http://www.mulesoft.org/schema/mule/munit-tools/current/mule-munit-tools.xsd">

    <munit:config name="demo-test-suite.xml" />

    <munit:test name="demo-test-suite-demo-main-BAD_REQUEST-Test" doc:id="8b6ba2d1-4696-43de-a07b-d854f4e5e2b6" description="Test" expectedErrorType="APIKIT:BAD_REQUEST">

        <munit:behavior >

            <munit-tools:mock-when doc:name="Mock when APIKIT:BAD_REQUEST" doc:id="6e68f7aa-75bf-4f15-93be-0c451375e9d9" processor="apikit:router">

                <munit-tools:with-attributes >

                    <munit-tools:with-attribute whereValue="demo-config" attributeName="config-ref" />

                </munit-tools:with-attributes>

                <munit-tools:then-return >

                    <munit-tools:error typeId="APIKIT:BAD_REQUEST" />

                </munit-tools:then-return>

            </munit-tools:mock-when>

        </munit:behavior>

        <munit:execution >

            <flow-ref doc:name="Flow-ref to demo-main" doc:id="9df72122-506f-4759-aa24-134f8341b782" name="demo-main"/>

        </munit:execution>

        <munit:validation >

            <munit-tools:assert-equals doc:name="Assert equals" doc:id="93698feb-de93-40b2-bc11-44739ad55c00" actual="#[vars.httpStatus]" expected="#[400]"/>

        </munit:validation>

    </munit:test>

    <munit:test name="demo-test-suite-demo-main-NOT_FOUND-Test" doc:id="8cfe733b-449f-4df1-b09d-06d79f63d8fb" description="Test" expectedErrorType="APIKIT:NOT_FOUND">

        <munit:behavior >

            <munit-tools:mock-when doc:name="Mock when APIKIT:NOT_FOUND" doc:id="ff561c10-4a97-456a-af92-29c144363d3d" processor="apikit:router">

                <munit-tools:with-attributes >

                    <munit-tools:with-attribute whereValue="demo-config" attributeName="config-ref" />

                </munit-tools:with-attributes>

                <munit-tools:then-return >

                    <munit-tools:error typeId="APIKIT:NOT_FOUND" />

                </munit-tools:then-return>

            </munit-tools:mock-when>

        </munit:behavior>

        <munit:execution >

            <flow-ref doc:name="Flow Reference" doc:id="5079c1a2-74ff-4591-ac6d-840b8b15e78a" name="demo-main"/>

        </munit:execution>

        <munit:validation >

            <munit-tools:assert-equals doc:name="Assert equals" doc:id="596dd171-6c28-415b-8a9d-8b9152f62cb4" actual="#[vars.httpStatus]" expected="#[404]"/>

        </munit:validation>

    </munit:test>

    <munit:test name="demo-test-suite-demo-main-METHOD_NOT_ALLOWED-Test" doc:id="635ace96-1503-40ce-8de3-13e7411726c8" description="Test" expectedErrorType="APIKIT:METHOD_NOT_ALLOWED">

        <munit:behavior >

            <munit-tools:mock-when doc:name="Mock when APIKIT:METHOD_NOT_ALLOWED" doc:id="894571a7-42b9-4caf-9be4-3c16d9cfa0f6" processor="apikit:router">

                <munit-tools:with-attributes >

                    <munit-tools:with-attribute whereValue="demo-config" attributeName="config-ref" />

                </munit-tools:with-attributes>

                <munit-tools:then-return >

                    <munit-tools:error typeId="APIKIT:METHOD_NOT_ALLOWED" />

                </munit-tools:then-return>

            </munit-tools:mock-when>

        </munit:behavior>

        <munit:execution >

            <flow-ref doc:name="Flow-ref to demo-main" doc:id="b57e4856-e333-4b45-bb01-c234f8e5e9f9" name="demo-main"/>

        </munit:execution>

        <munit:validation >

            <munit-tools:assert-equals doc:name="Assert equals" doc:id="493307c6-825b-4652-a61e-2973332a972f" actual="#[vars.httpStatus]" expected="#[405]"/>

        </munit:validation>

    </munit:test>

    <munit:test name="demo-test-suite-demo-main-NOT_ACCEPTABLE-Test" doc:id="1bec6f0d-01ed-43b2-bf39-8736cbf5e590" description="Test" expectedErrorType="APIKIT:NOT_ACCEPTABLE">

        <munit:behavior >

            <munit-tools:mock-when doc:name="Mock when APIKIT:NOT_ACCEPTABLE" doc:id="3a329baf-99df-49fb-8b18-636dc5316973" processor="apikit:router">

                <munit-tools:with-attributes >

                    <munit-tools:with-attribute whereValue="demo-config" attributeName="config-ref" />

                </munit-tools:with-attributes>

                <munit-tools:then-return >

                    <munit-tools:error typeId="APIKIT:NOT_ACCEPTABLE" />

                </munit-tools:then-return>

            </munit-tools:mock-when>

        </munit:behavior>

        <munit:execution >

            <flow-ref doc:name="Flow Reference" doc:id="a46ee38a-13b0-431d-ab94-6fafa8492ee2" name="demo-main"/>

        </munit:execution>

        <munit:validation >

            <munit-tools:assert-equals doc:name="Assert equals" doc:id="35aff4f1-30e8-4cda-b71e-736d0b279a98" actual="#[vars.httpStatus]" expected="#[406]"/>

        </munit:validation>

    </munit:test>

    <munit:test name="demo-test-suite-demo-main-UNSUPPORTED_MEDIA_TYPE-Test" doc:id="d8454189-e4e1-4375-b585-0a999a837619" description="Test" expectedErrorType="APIKIT:UNSUPPORTED_MEDIA_TYPE">

        <munit:behavior >

            <munit-tools:mock-when doc:name="Mock when APIKIT:UNSUPPORTED_MEDIA_TYPE" doc:id="f41c89d8-b712-47a4-b358-ef8cb3ba086c" processor="apikit:router">

                <munit-tools:with-attributes >

                    <munit-tools:with-attribute whereValue="demo-config" attributeName="config-ref" />

                </munit-tools:with-attributes>

                <munit-tools:then-return >

                    <munit-tools:error typeId="APIKIT:UNSUPPORTED_MEDIA_TYPE" />

                </munit-tools:then-return>

            </munit-tools:mock-when>

        </munit:behavior>

        <munit:execution >

            <flow-ref doc:name="Flow-ref to demo-main" doc:id="92675038-645a-4a98-b897-06845a04e6e3" name="demo-main"/>

        </munit:execution>

        <munit:validation >

            <munit-tools:assert-equals doc:name="Assert equals" doc:id="5065fd21-8f7f-4d1f-a6d5-042f40aefbfd" actual="#[vars.httpStatus]" expected="#[415]"/>

        </munit:validation>

    </munit:test>

    <munit:test name="demo-test-suite-demo-main-NOT_IMPLEMENTED-Test" doc:id="e2cbf56e-ee66-41cc-82be-333dd1eb292c" description="Test" expectedErrorType="APIKIT:NOT_IMPLEMENTED">

        <munit:behavior >

            <munit-tools:mock-when doc:name="Mock when APIKIT:NOT_IMPLEMENTED" doc:id="aeb6b8f2-7488-4608-b591-ee22331b85f0" processor="apikit:router">

                <munit-tools:with-attributes >

                    <munit-tools:with-attribute whereValue="demo-config" attributeName="config-ref" />

                </munit-tools:with-attributes>

                <munit-tools:then-return >

                    <munit-tools:error typeId="APIKIT:NOT_IMPLEMENTED" />

                </munit-tools:then-return>

            </munit-tools:mock-when>

        </munit:behavior>

        <munit:execution >

            <flow-ref doc:name="Flow-ref to demo-main" doc:id="bd189bb8-d517-448c-bcdf-fe043190d0a0" name="demo-main"/>

        </munit:execution>

        <munit:validation >

            <munit-tools:assert-equals doc:name="Assert equals" doc:id="302886a9-6dc6-4c96-920f-0d6f4100aa9c" actual="#[vars.httpStatus]" expected="#[501]"/>

        </munit:validation>

    </munit:test>

</mule>

Testing API Flow (web browser) Test case

Opinions expressed by DZone contributors are their own.

Related

  • Error Handling Inside Kumologica Subflow
  • Testing Jedis API Using Junit Test Case in Eclipse IDE
  • How to Build an API Testing Program with MuleSoft Anypoint
  • Mocking and Its Importance in Integration and E2E Testing

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!