Building and unit testing Mule project with Maven

Mule is a project management tool to manage dependency and you can use it to develop a project and test it.

1. Create a mule project and select use Maven checkbox to enable maven support to the project and click finish. Maven will start downloading the dependency it requires to build a project based on pom.xml

example:  project1






































2. Create a mule flow by adding a http connector and set payload
3. Add the http listener configurations details as below
        Host: Localhost
        Port :8081
        path: /hello
       Allowed Method: GET

4. Configure the Set Payload component to set the value to "Hello World!"




















Once you add http connector Maven adds the dependency in pom.xml and  automatically download its required dependencies

        <dependency>
            <groupId>org.mule.modules</groupId>
            <artifactId>mule-module-http</artifactId>
            <version>${mule.version}</version>
            <scope>provided</scope>
        </dependency>

Right click the mule project and Run as Mule Application with maven, watch for console for BUILD SUCCESS

5. Test the project using postman and see if you can able to invoke successfully

You can create and run the test cases by extending FunctionTestCase class.
FunctionalTestCase is a base test case for Mule functional tests. Your test cases can extend toFunctionalTestCase use its functionality.
FunctionalTestCase fires up a Mule server using a configuration you specify by overriding the getConfigResources()
create a java class TestHello.java by right click src/test/java folder and paste the below code


package project1;
import org.junit.*;
import java.util.*;
import org.mule.tck.junit4.FunctionalTestCase;
import org.mule.transport.NullPayload;
import org.mule.api.MuleMessage;
import org.mule.api.client.MuleClient; 
import static org.junit.Assert.*;
public class TestHello extends FunctionalTestCase {
public static String message="Hello World!";
@Test
public void clientTestCase() throws Exception{
MuleClient client=muleContext.getClient();
Map<String,Object> props=new HashMap<String,Object>();
props.put("http.method", "GET");
MuleMessage result=client.send("http://localhost:8081/hello", "",props);
assertNotNull(result);
assertFalse(result.getPayload() instanceof NullPayload);
assertEquals(message,result.getPayloadAsString());
}
@Override
protected String getConfigResources() {
// TODO Auto-generated method stub
return "src/main/app/project1.xml";
}
}
6. Save the project and and deploy the project to mule runtime.


++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2017-04-09 17:07:04,532 [main] org.mule.lifecycle.AbstractLifecycleManager: Starting model: _muleSystemModel
INFO  2017-04-09 17:07:04,535 [main] org.mule.construct.FlowConstructLifecycleManager: Starting flow: project1Flow
INFO  2017-04-09 17:07:04,537 [main] org.mule.processor.SedaStageLifecycleManager: Starting service: project1Flow.stage1
INFO  2017-04-09 17:07:04,641 [main] org.mule.module.management.agent.WrapperManagerAgent: This JVM hasn't been launched by the wrapper, the agent will not run.
INFO  2017-04-09 17:07:04,668 [main] org.mule.DefaultMuleContext: 
**********************************************************************
* Application: project1                                              *
* OS encoding: \, Mule encoding: UTF-8                               *
*                                                                    *
* Agents Running:                                                    *
*   DevKit Extension Information                                     *
*   JMX Agent                                                        *
*   Batch module default engine                                      *
*   Wrapper Manager                                                  *
**********************************************************************
INFO  2017-04-09 17:07:04,668 [main] org.mule.module.launcher.MuleDeploymentService: 
**********************************************************************
* Started app 'project1'                                             *
**********************************************************************
INFO  2017-04-09 17:07:04,683 [main] org.mule.module.launcher.DeploymentDirectoryWatcher: 
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
+ Mule is up and kicking (every 5000ms)                    +
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
INFO  2017-04-09 17:07:04,699 [main] org.mule.module.launcher.StartupSummaryDeploymentListener: 
**********************************************************************
*              - - + DOMAIN + - -               * - - + STATUS + - - *
**********************************************************************
* default                                       * DEPLOYED           *
**********************************************************************

*******************************************************************************************************
*            - - + APPLICATION + - -            *       - - + DOMAIN + - -       * - - + STATUS + - - *
*******************************************************************************************************
* project1                                      * default                        * DEPLOYED           *

*******************************************************************************************************


7. If you see build failure then run the maven qith debug options to find out the full stack trace.

Add -X to Maven configuration 



Comments

Popular posts from this blog

RingCentral - How to use ringcentral Webhook features for events in Java

Hello world project in Spring