Introduction to MUnit

Reading Time: 3 minutes

MUnit is a MuleSoft’s testing framework that allows developers to create, design and run unit tests on APIs and integrations created within Anypoint Studio. MUnit is fully integrated with Anypoint Studio, allowing you to create, design and run MUnit tests just like you would Mule applications. It provides a full suite of integration and unit test capabilities.

Features available with Mule MUnit

  • You can create and build Mule tests by writing Mule code
  • You can create and build Mule tests by writing Java code.
  • You can verify Message Processor calls.
  • You can mock Message Processor.
  • You can debug the MUnit tests.
  • You can Generate and view MUnit tests coverage reports
  • You can asserts data like Assert Equals, Assert Not Equals, Assert Payload, Assert False, Assert True, Assert Null Payload, Assert Not Null Payload.
  • You can Spy Event Processor.

Installation of MUnit 

To install MUnit in Anypoint studio please follow these steps.

Go to Help –> Install New Software…

It will open a new pop-up window. Type MUnit and you will see the MUnit Update Site along with the URL. Choose one of the URLs.

Please check the MUnit checkbox and click on Next Button.

You have now successfully installed MUnit.

Dependencies

MUnit is divided in two main sub modules.

  • MUnit
  • MUnit Tools

Each module has its own dependency

MUnit Dependencies

<dependency>
	<groupId>com.mulesoft.munit</groupId>
	<artifactId>munit-runner</artifactId>
	<version>2.3.0</version>
	<classifier>mule-plugin</classifier>
	<scope>test</scope>
</dependency>

MUnit Tools dependencies

<dependency>
	<groupId>com.mulesoft.munit</groupId>
	<artifactId>munit-tools</artifactId>
	<version>2.3.0</version>
	<classifier>mule-plugin</classifier>
	<scope>test</scope>
</dependency>

MUnit Operations

  • Test: It creates a new test and “flow” broken down into three sections Execution, Behavior and Validation. The behavior scope sets all the preconditions before executing the test logic. The execution scope contains the testing logic which will wait for all processes to finish before executing the next scope. The validation scope contains all the validations for the results of the execution scope.
  • Set Event: It allows you to define a Mule Event. Usually used at the beginning of an MUnit test to define the first message to send to the flow being tested.
  • Set null payload: It defines a null payload to test how your flow will handle a null value.
  • After Suite: It runs after executing all of the MUnit tests and runs just once. For example, let’s suppose you have an MUnit Test Suite File with four tests. The code inside an MUnit After Suite, runs just once, after all of your tests.
  • After Test: It runs after executing an MUnit test. For example, let’s suppose you have an MUnit Test Suite file with four tests. The code inside an MUnit After Test runs after each of your four tests; it runs four times.
  • Before Suite: It runs before executing all of the MUnit tests and runs just once. For example, suppose you have an MUnit Test Suite file with four tests. The code inside an MUnit Before Suite runs just once, before all of your four tests.
  • Before Test: It runs before executing an MUnit test. For example, let’s suppose you have an MUnit Test Suite file with four tests. The code inside an MUnit Before test runs before each of your four tests it runs four times.

MUnit Tools

  • Assert That: It Allows you to run assertions to validate the state of a Mule event, such as checking to see if a payload equals a certain value.
  • Fail: It’s useful in validating that a test should fail if that point is reached in the flow. The processor throws a java.lang.AssertionError.
  • Mock When: You can use this processor to mock an event, such as sending a mock POST request with a mocked payload.
  • Run Custom: It allows you to assert the Mule event content against a custom assertion.
  • Verify Call: It allows you to verify if a processor was called.
  • Spy: It allows you to see what happens before and after an event processor.

References

https://docs.mulesoft.com/munit/2.3/

Written by 

Gaurav Dubey is a Java Developer at Knoldus Software LLP. He has done M.CA from Kurukshetra University and completed Bachelor's Degree in Computer Science from M. D. University. He is a tech enthusiast with good knowledge of Java. He is majorly focused in Java practice. On the personal front, he loves to cook and travel.

Leave a Reply