Alexandra Martinez
What is MUnit and how to create
your first unit test
2
● About me
● What is unit testing / MUnit
● Basic MUnit components for unit testing
● Demo
● Questions
Agenda
3
● Senior MuleSoft Developer
● MuleSoft Ambassadress & Meetup Leader
● 5x MuleSoft Certified
● 3+ years using MuleSoft
● Evangelist at Women Who Code Monterrey
● Founder and Content Creator at ProstDev
About me
4
● Type of Software Testing to verify that your code works as expected.
● Create code to test your code 
What is unit testing?
UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST
PASSED?
1 0 PLUS 1 1 1
5
● Type of Software Testing to verify that your code works as expected.
● Create code to test your code 
What is unit testing?
UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST
PASSED?
1 0 PLUS 1 1 1
2 87 PLUS 20 107 107
6
● Type of Software Testing to verify that your code works as expected.
● Create code to test your code 
What is unit testing?
UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST
PASSED?
1 0 PLUS 1 1 1
2 87 PLUS 20 107 107
3 -2 PLUS -2 4 -4
7
● MuleSoft’s framework to create Unit Tests for Mule flows.
● Drag-and-drop / visual components.
● FAIL / PASS visual results.
What is MUnit?
8
● Better quality code
● Final user is happy
● Other developers are happy
● Less time spent fixing bugs after release
● Less budget spent on bug fixes
● Final user is happy
● Other developers are happy
● Project/Delivery/Product Managers are happy
Why do we want to test our code?
(Set Event, Mock, Assert)
Basic MUnit components for unit
testing
10
● Mule Event
● Payload
● Attributes
● Error
● Variables
Mule Event
Error
11
● Set a new Mule Event
● Payload
● Attributes
● Error
● Variables
Set Event
Error
12
Set Event: Example
Error
Set a new Mule Event containing this payload:
Set Event
and containing a variable called isActive set to true.
13
1) Mock when <processor>
2) Then return <Mule Event>
“Imagine that when we try to call that,
we get this Mule Event.”
Mock when
Error
14
1) Mock when <processor>
2) Then return <Mule Event>
“Imagine that when we try to call Salesforce
via HTTP, we get this JSON payload.”
Mock when: Example
Error
1) Mock when processor’s name is “HTTP Request to
Salesforce”
2) Then return a new Mule Event containing this
payload:
Mock when
15
● Assert equals
● Assert that
● Assert expression
”Let’s make sure that this is equal to that.”
“Let’s make sure that this is not empty.”
“Let’s make sure that this is more than that.”
Assert
Error
16
● Assert that
”Let’s make sure that the payload
is equal to this JSON object.”
If they are equal, the test passed.
If not, it failed.
Assert: Example
Error
Assert that the actual payload
is equal to this JSON object:
Assert that
Is this true?
DEMO TIME!
Continue learning
GitHub Repository:
https://github.com/alexandramartinez/vampireAPI
Knowledge check!
1. Take a look at how the MUnit test is built
2. Remove the MUnit test suite
3. Try to build it again!
4. (Optional) Add more tests/validations
Documentation:
● MUnit Event Processors: https://docs.mulesoft.com/munit/2.3/event-processors
● Set Event: https://docs.mulesoft.com/munit/2.3/set-event-processor
● Mock When: https://docs.mulesoft.com/munit/2.3/mock-event-processor
● Assert That: https://docs.mulesoft.com/munit/2.3/assertion-event-processor
Questions?
Contact me!
Website:
www.alexandramartinez.world
LinkedIn:
www.linkedin.com/in/alexandra-n-martinez
Twitter:
@alexmartinez_z
ProstDev
Thank you!

What is munit and how to create your first unit test

  • 1.
    Alexandra Martinez What isMUnit and how to create your first unit test
  • 2.
    2 ● About me ●What is unit testing / MUnit ● Basic MUnit components for unit testing ● Demo ● Questions Agenda
  • 3.
    3 ● Senior MuleSoftDeveloper ● MuleSoft Ambassadress & Meetup Leader ● 5x MuleSoft Certified ● 3+ years using MuleSoft ● Evangelist at Women Who Code Monterrey ● Founder and Content Creator at ProstDev About me
  • 4.
    4 ● Type ofSoftware Testing to verify that your code works as expected. ● Create code to test your code  What is unit testing? UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST PASSED? 1 0 PLUS 1 1 1
  • 5.
    5 ● Type ofSoftware Testing to verify that your code works as expected. ● Create code to test your code  What is unit testing? UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST PASSED? 1 0 PLUS 1 1 1 2 87 PLUS 20 107 107
  • 6.
    6 ● Type ofSoftware Testing to verify that your code works as expected. ● Create code to test your code  What is unit testing? UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST PASSED? 1 0 PLUS 1 1 1 2 87 PLUS 20 107 107 3 -2 PLUS -2 4 -4
  • 7.
    7 ● MuleSoft’s frameworkto create Unit Tests for Mule flows. ● Drag-and-drop / visual components. ● FAIL / PASS visual results. What is MUnit?
  • 8.
    8 ● Better qualitycode ● Final user is happy ● Other developers are happy ● Less time spent fixing bugs after release ● Less budget spent on bug fixes ● Final user is happy ● Other developers are happy ● Project/Delivery/Product Managers are happy Why do we want to test our code?
  • 9.
    (Set Event, Mock,Assert) Basic MUnit components for unit testing
  • 10.
    10 ● Mule Event ●Payload ● Attributes ● Error ● Variables Mule Event Error
  • 11.
    11 ● Set anew Mule Event ● Payload ● Attributes ● Error ● Variables Set Event Error
  • 12.
    12 Set Event: Example Error Seta new Mule Event containing this payload: Set Event and containing a variable called isActive set to true.
  • 13.
    13 1) Mock when<processor> 2) Then return <Mule Event> “Imagine that when we try to call that, we get this Mule Event.” Mock when Error
  • 14.
    14 1) Mock when<processor> 2) Then return <Mule Event> “Imagine that when we try to call Salesforce via HTTP, we get this JSON payload.” Mock when: Example Error 1) Mock when processor’s name is “HTTP Request to Salesforce” 2) Then return a new Mule Event containing this payload: Mock when
  • 15.
    15 ● Assert equals ●Assert that ● Assert expression ”Let’s make sure that this is equal to that.” “Let’s make sure that this is not empty.” “Let’s make sure that this is more than that.” Assert Error
  • 16.
    16 ● Assert that ”Let’smake sure that the payload is equal to this JSON object.” If they are equal, the test passed. If not, it failed. Assert: Example Error Assert that the actual payload is equal to this JSON object: Assert that Is this true?
  • 17.
  • 18.
    Continue learning GitHub Repository: https://github.com/alexandramartinez/vampireAPI Knowledgecheck! 1. Take a look at how the MUnit test is built 2. Remove the MUnit test suite 3. Try to build it again! 4. (Optional) Add more tests/validations Documentation: ● MUnit Event Processors: https://docs.mulesoft.com/munit/2.3/event-processors ● Set Event: https://docs.mulesoft.com/munit/2.3/set-event-processor ● Mock When: https://docs.mulesoft.com/munit/2.3/mock-event-processor ● Assert That: https://docs.mulesoft.com/munit/2.3/assertion-event-processor
  • 19.
  • 20.
  • 21.