Friday, June 16, 2017

MuleSoft Integration Testing with Postman and Newman CLI

1.0 Overview

There are two types of testing when it comes to integration software development and they are namely unit testing and integration testing.

Unit testing in mule can be realized by Junit testing and Munit testing. You could actually use Munit to do integration testing to a certain degree. Integration testing could also be done by using SOAP UI and CURL.

But in this article we are going to talk about integration testing using Postman and Newman CLI, I will use the following sections as a step by step walk through to teach you on how this can be done.

2.0 Creating the Mule Application

The mule application we are going to create is a simple flow with a HTTP receive endpoint, as shown in Figure 2.0a

Figure 2.0a
Figure 2.0b shows the content/configuration of the logger message processor.

Figure 2.0b
The following is the full Mule XML configuration for the application.
<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
       xmlns:spring="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd">
    <flow name="postmanautomatedtestFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/postman" doc:name="HTTP"/>
        <byte-array-to-string-transformer doc:name="Byte Array to String"/>
        <logger message="Postman Automated Test #[payload + &quot; &quot;] #[message.id]" level="INFO" doc:name="Logger"/>
    </flow>
</mule>

Message.id is unique and it will be created for each new request. I have purposefully logged it down to show you that each time you see a log in the console it would mean that a new external request has been triggered.

Once you have create a similar HTTP receive endpoint flow like mine in Anypoint studio, proceed to start the application.

3.0 Testing with Postman

I have create a youtube video on testing mule application with postman, you can view the video if you want to see it, otherwise just read my following instructions.
1)      In order to test the created mule application you need to start up Postman and point it to the following URL http://localhost:8082/postman, change from get to post and put the string literal “testing” in the payload text area.



2)      The next step is to click send, you will then now get a 200 response, with the same string literal that you have previously entered because the mule application does nothing but logs your request to the anypoint studio console, now notice the status code that is returned is 200 OK.



3)      Next we are going to the test, at the top of Postman screen just below the address bar as shown in the following picture.



4)      Once you are in the test, text area key in the following assert statements.
tests["Status code is 200"] = responseCode.code === 200;
It will look like the following print screen. There are a lot assert snippets in which you could explore on the right side of your screen.



5)      Now when you click on send button again notice that now you will see Test(1/1) at the bottom of the result pane.



6)      If you change your assert response code to 400 instead of 200, and you click send again, you will notice test is now Test(0/1), which means test has failed, change the assert back to 200 as stated in step 4)



7)      Now save the test into a collection, go to the top right conner of the Postman screen click on the save button and select the save as menu.



8)      A save request pop up dialog would appear, now key in the following inputs, and proceed to click save.



9)      Once you have save your test into a collection, notice that you will see, the previously saved collection and the test on the far left, collections tab as depicted in the following illustration.



10)   Now I need to you export the saved collection into a JSON file, this is so that we could use Newman Command Line Interface (CLI) to trigger it. I order to export the save test into JSON file, click on the ellipsis button and select export from the menu.



11)   An Export Collection dialog box would pop-up, here select Collection V2 radio button and click export button, and save to a folder that is easily accessible, we will use this file in the following section.




4.0 Newman CLI Testing using Postman collection export

If you don’t have Newman installed you need to install it via NPM, if you don’t have NPM installed you need to install it first. I am using window lap top so I am going to launch the windows command prompt to execute Neman CLI.
Navigate to the folder containing your exported JSON file, if you open the JSON file you will see the following as the contents of the file.
{
                        "variables": [],
                        "info": {
                                                "name": "PostmanAutomatedTestDemo",
                                                "_postman_id": "75d716b9-e83b-ae2c-91dd-d5ae7ccf4327",
                                                "description": "",
                                                "schema": "https://schema.getpostman.com/json/collection/v2.0.0/collection.json"
                        },
                        "item": [
                                                {
                                                                        "name": "TestingHTTPMuleFlow",
                                                                        "event": [
                                                                                                {
                                                                                                                        "listen": "test",
                                                                                                                        "script": {
                                                                                                                                                "type": "text/javascript",
                                                                                                                                                "exec": [
                                                                                                                                                                        "tests[\"Status code is 200\"] = responseCode.code === 200;"
                                                                                                                                                ]
                                                                                                                        }
                                                                                                }
                                                                        ],
                                                                        "request": {
                                                                                                "url": "localhost:8082/postman",
                                                                                                "method": "POST",
                                                                                                "header": [],
                                                                                                "body": {
                                                                                                                        "mode": "raw",
                                                                                                                        "raw": "testing "
                                                                                                },
                                                                                                "description": ""
                                                                        },
                                                                        "response": []
                                                }
                        ]
}

Launch windows command prompt and execute the following command.
newman run PostmanAutomatedTestDemo.postman_collection.json -n 10

Once you have finished newman has finished running your test the following result table would be printed on your command prompt.



The “–n 10” option in the newman cli means run this test 10 times, you will be able to see the same thing being reflected at the anypoint console where by 10 lines will be logged, and each line has a unique message id.



5.0 Conclusion

I have shown you in a few simple steps you could set up automated integration testing, one practical thing you could do with this is load testing. If you have an API that is exposed, via a HTTP receive endpoint, you could essentially simulate load testing by running few command prompts concurrently each with a few hundred or even thousands of iteration.

8 comments:

  1. Great Site, The Course were so simple and easy to understand.
    Mulesoft Online Training

    ReplyDelete
  2. For Mulesoft ESB Real Time Training Contact Me at muleesbtrainer@gmail.com and 9985333565

    I will provide real time online training at low price for demo videos visit my youtube channel.
    http://youtube.com/channel/UC1lFGEh4xAPJnIm4FWmQBfA

    ReplyDelete
  3. This is blog is helpful for everyone. Thanks for sharing, keep on sharing this type of articles

    Mulesoft Online Training
    Mulesoft Training in Hyderabad

    ReplyDelete
  4. Thanks for sharing valuable information and very well explained. keep posting.

    mulesoft training courses
    mulesoft online course

    ReplyDelete