mule logo

How to run Junit test with mule

Doing Junit with mule is pretty simple. You only need to extend the class

1
org.mule.tck.junit4.FunctionalTestCase

here is a simple example :

package product_registration;

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
import static org.junit.Assert.assertNotNull;

import org.junit.Test;
import org.mule.api.MuleException;
import org.mule.api.MuleMessage;
import org.mule.api.client.MuleClient;
import org.mule.tck.junit4.FunctionalTestCase;

public class ProductServiceFunctionalTestCase extends FunctionalTestCase{
    protected String getConfigResources(){
        System.out.println("hello world");
        return "./src/main/app/product_registration.xml";  
    }
   
    @Test
    public void testCanRegisterProducts() throws MuleException{
        MuleClient client = muleContext.getClient();
       
        String productAsJson = "    {"name":"Widget",        "price": 9.99,"weight": 1.0,"sku": "abcd-12345"}";
   
        client.dispatch("http://localhost:8080/products", productAsJson,null);
       
        MuleMessage result = client.request("jms://products", RECEIVE_TIMEOUT);
       
        assertNotNull(result);
    }
}