Using littleproxy in Mule unit tests

My mule configurations files often contain proxy settings for connectors that are used to communicate to the outside world. However during development and testing I can’t use the normal proxy, since i’m mocking the outside world.

Littleproxy is a java proxy that you can use to replace the real proxy with a testing proxy. This avoids you having to install a proxy on your development machine and on the continuous integration server.

The following is part of a mule config. It defines an http connector that is configured to talk through a proxy.

<http:connector name="http.connector"/>
<http:connector name="http.connector.proxy" 
                proxyHostname="localhost" proxyPort="50103"/>
<flow name="http.gateway.and.proxy">
    <http:inbound-endpoint host="localhost" port="50102" 
    <logger message="http.gateway - #[groovy:message.toString()]"/>
    <http:outbound-endpoint host="localhost" port="50100" 

Include little proxy in your maven pom.xml:


Here’s the unit test. First i start the proxy (you could also use @Before or @BeforeClass), and then i run a regular mule unit test:

public void testShouldReturnHelloWorldThroughGatewayAndProxy() 
    throws Exception {
    String msg = "Hi!";
    HttpProxyServer proxyServer = new DefaultHttpProxyServer(50103);
    * send message
    MuleClient client = new MuleClient(muleContext);
    MuleMessage result = client.send(
            , msg, null);
    String payload = result.getPayloadAsString();
    Assert.assertTrue("Did not receive HelloWorld!: " + payload, "HelloWorld!".equals(payload));
blog comments powered by Disqus