Munit In Mule 4 | Patna MuleSoft Meetup #26

shyamraj55 257 views 29 slides Feb 03, 2024
Slide 1
Slide 1 of 29
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15
Slide 16
16
Slide 17
17
Slide 18
18
Slide 19
19
Slide 20
20
Slide 21
21
Slide 22
22
Slide 23
23
Slide 24
24
Slide 25
25
Slide 26
26
Slide 27
27
Slide 28
28
Slide 29
29

About This Presentation

Munit In Mule 4 | Patna MuleSoft Meetup #26


Slide Content

Patna MuleSoft Meetup

Munit in Mule 4

AGENDA
●Introductions
●What Munit is and what you can do with Munit?
●Demo
●Q/A

Organizers
Om PrakashShyam Raj Prasad Amit Mohan

Speakers
Piyush Kumari
●Working as Senior Consultant at InvenioLSI.
●Approx 7 years of Total Experience.
●6+ years experience in Mulesoft.
●Certified MuleSoft Developer.

Munit Introduction

What is Munit?
●Best Automated testing app in Anypoint Studio.
●Integrated with Anypoint studio, allow you to create, design and run Munits just like you
would mule application.
●Provides full coverage of code.
●Integrated with Maven and surefire with integrations with continuous deployment
environment.

With MUnits you can ?
Munits proves below features:

●Create you test cases by writing Mule code.
●Mock Processors(Http Requester, SAP Connector, ApiKit Router etc.)
●Spy on any processor
●Parameterized test cases based on types.
●Do end to end or particular at sub-flow level of testing.
●Check coverage in studio
●Get Coverage Reports.

Munits is divided into two main sub modules:

Munit operations:
●Before Suite: runs before executing Munit Test Suite
The MUnit Before Suite contains code that is meant to be executed before the whole suite.
The Before Suite has one execution rule: Run before all the tests, 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 your four tests

●After Suite:
The MUnit After Suite is a scope. It can contain code that is meant to be executed after the
whole suite. The After suite has one execution rule: Run after all the tests, just once.
For instance, 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 your tests.

Munit operations:
●Before Test: runs BEFORE executing Munit Test.
The MUnit Before Test Scope contains code that is meant to be executed before each test. Each
Before Test scope follows the same execution rule: Run before each test.

●After Test: runs AFTER executing Munit Test.
The MUnit After Test Scope contains code that is meant to be executed after each test. Each After
Test scope follows the same execution rule: Run after each test.
●Test: adds a new test.

●Set Event: to set the initial payload to be supplied.

●Set null Payload: defines null payload for testing.

Munit Tools operations:
●Assert: validate mule event content.
○Equals
○Expression
○That
●Mock When: mock event processor when it matches the
defined name and attributes.
●Spy: what happens when before/after event processor is
called.
●Verify Call: verify if the processor was called.
●Fail: allow you to fail you test case on purpose.

Various ways to create munit tests:
Method 1: File>New>MUnit Test

Various ways to create munit tests:
Method 2: Pallet Test Operation

Various ways to create munit tests:
Method 3: From Flow



Result: XML Creation and munit test suite

MUnit Tests Scope:


Behaviour Scope:
-Set all the conditions before executing test logic
-Mock and spy goes into this section

Execution scope:
-This is meant for testing the logic which will wait for all the processes to get completed before
executing the next scope.
-Runs flow through this section only.

Validation Scope:
-This is meant to have all the validations regarding the result of execution scope.
-Assert and verify comes here to validate and verify the flow.

MUnit Tests Scope:


-Scope looks like:

Set Event:


●Allows you to set payload,attributes and
variables.
●Normally defined at the beginning of munit test
to define the first message to set.
●You can define as many properties required for
you flow.

Assert That:


●Allows you to run assertions to validate the stats of event
content.
●Uses DataWeave function MUnit matchers to define the
conditions for any value in expression.
●Include the prefix MunitTool:: in the expression
○notNullValue() checks that the expression is not null.
■#[MunitTools::nullValue()]

Matchers:
●withMediaType(String)
#[MunitTools::withMediaType('text/xml')]

●containsString(String)
#[MunitTools::containsString('example')]

●greaterThan(Comparable)
#[MunitTools::greaterThan(|2017-08-09|)]"

●isEmpty()
#[MunitTools::isEmpty()]"

●Core Matchers: evaluate the core
value in the expression

●String Matchers: performs the
assertions on string expression.

●Comparable Matchers: compares the
expression against the specified value.

●Iterable and Map Matchers: when your
expression is an array or map data.
Examples:

Mock When:
Allows you to Mock an event processor when it matches the
defined name and attributes.
1.Pick Processor
2.Choose Target Processor
3.Select attributes(specific doc:name)
4.Click OK

Mock When:
Configure “Then Return “ Section:
●Then Return is defined to set the response which
we expect from the mocked processor.
●The response can be Payload, Variable, Attribute
or even an error.
1.Choose Type of response
2.Click Add Icon
3.Define Response
4.Click OK

SPY: Work as detective to check when happens
before and after the processor.
1.Pick Processor
2.Choose Target Processor
3.Select attributes(specific
doc:name)
4.Click OK

Verify Call: Allows you to verify is that particular processor
was called or not.

●Defining one test suite and then multiple test cases inside with multiple Munit:Test
Ways to write Munit Test Suite:

●Parametrizations:
Parameterization in munits allows you to run single test
with multiple cases(inputs and outputs).


PROS:
Developers can effectively cover
various logical flows without having
to write dedicated test flows for each
such logical flow
CONS:
Application will get deployed exactly
the number of parameterized test
cases written.

3(parametrization case) → 3 times
application will deploy.

●Basic Info:
1.To run single Munit from terminal:
i.mvn clean /munit test name/ -Dmunit.test=XYZ.xml#testName
2.Ignore particular flow or file from munit coverage:
<coverage>
<ignoreFlows>
<ignoreFlow>flow-name</ignoreFlow>
</ignoreFlows>
</coverage>

3.To run API skipping munits in local:
Maven clean package/mvn clean install -DskipTests

Demo

Networking time

Thank You