Cucumber framework with Mule ESB

What is Mule ESB ?

Mule ESB is a lightweight Java-based enterprise service bus (ESB) and integration platform that allows developers to connect applications together quickly and easily, enabling them to exchange data. Mule ESB enables easy integration of existing systems, regardless of the different technologies that the applications use, including JMS, Web Services, JDBC, HTTP, and more.

This example shows how we can integrate Cucumber test framework with Mule ESB for integration or acceptance or Behavioral Driven Development(BDD). We simply put a file in a source directory and Mule will read the file from the source directory. Using Mule ESB it’s very easy to read the file from a location. If we had to read the file using manual coding then we had to write many lines of code. But using Mule ESB we just simply put a file in a directory and let the Mule know the file path and Mule does the rest of the thing. You can put any kind of file to the source for reading. The file that we put into a source directory will be an attachment to the intended recipient’s mail address. So finally when we will see the file as an attachment in the mailbox.

What is Cucumber Framework ?

Please have a look at https://cukes.info/

Prerequisites

Anypoint Studio(Mule Studio) 3.5.1
JDK 1.6
Maven 3
Mule Runtime 3.5.1 EE
Knowledge of XML, Java, Cucumber

Step 1. Open Mule Studio and create a mule project. Go to File->New->Mule Project. Now enter the Project Name, select Runtime – Mule Server, check the check box for Use Maven. Now enter the following information

Group Id: your company(ex. roytuts.com) name – com.roytuts
Artifact Id: project name – mulecucumber(automatically populated when project name is getting typed)
Version: Mule studio generates default

Step 2. Click Next and verify project Location and JRE/JDK are set correctly. Click Finish.

Step 3. Now drag and drop the elements from right side of the Mule studio as shown below in the picture.

Cucumber Framework with Mule ESB

File connector properties

File connector properties Browse and select the path from where a file will be read for an attachment. Logger component properties Logger component will display content in console from each flow. File to String transformer properties File-to-Strong transformer just transforms the file object to human readable string format. We will not set any property for this element. Attachment transformer properties Using attachment transformer we can set payload from previous flow as an attachment. We have to select Content Type, operation as Set Attachment, Name, Value. SMTP connector properties For SMTP connector we have to input email host, user name, password, port under Basic Settings and To Address, From Address, Subject under Email Information. The whole XML file of entire flow

Browse and select the path from where a file will be read for an attachment.

File to String transformer properties

File-to-Strong transformer just transforms the file object to human readable string format. We will not set any property for this element.

Logger component properties

Logger component will display content in console from each flow.

The whole XML file of entire flow

Step 4. Once we put any file under the selected path from where mule will read the file, we may run the mulecucumber.xml file and we will see the file output in the console but it is not our main target. We want to test the flow using Cucumber framework. So we will now create all those required things for the Cucumber framework.

We will put the mulecucumber.xml file under src/test/resources/mule directory. If src/test/resources does not exist then create it from New->Source Folder.

Step 5. We will create mule-config.xml file with below content and put it under src/test/app directory.

Step 6. Now we will create a feature file called CucumberFeature.feature with the below content inside it and put it under src/test/resources/feature.

Step 7. We will create utility class MuleEmbeddedServer.java and put it under src/test/java directory in the package com.roytuts.cucumber.common with below content.

Step 8. Now we will create test class which will test the Mule flow using Cucumber framework.

In the above class features = { “classpath:features/CucumberFeature.feature” } means that we have to put the feature file under src/test/resources/feature.
glue = “com.roytuts.cucumber.cuke.steps” means we will have a step class file under src/test/java directory in the package com.roytuts.cucumber.cuke.steps.

Step 9. Now we will create blank CucumberSteps.java file under src/test/java directory in the package com.roytuts.cucumber.cuke.steps.

Step 10. Now we will run the class file CucumberTest.java and we will get the steps signatures in the console. So we will copy those unimplemented steps into the class CucumberSteps.java and implement them one by one as shown below.

Step 11. As we have shown the value for INBOUND_FILE is files/data.csv. So we will put a sample data.csv file under src/test/resources/files.

Step 12. Now run the class file CucumberTest.java you will get the output at console as shown below.

That’s all. Thanks for reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.