What is munit and how to create your first unit test

AlexandraNahomiMartn 109 views 21 slides Feb 26, 2021
Slide 1
Slide 1 of 21
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

About This Presentation

What is MUnit? What is unit testing? Why is this important? How to get started with MUnit...


Slide Content

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? A B C PLUS = UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST PASSED? 1 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? A B C PLUS = UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST PASSED? 1 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? A B C PLUS = UNIT TEST # A PLUS B ACTUAL C EXPECTED C TEST PASSED? 1 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 M U nit?

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 M U nit 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! Take a look at how the MUnit test is built Remove the MUnit test suite Try to build it again! (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!