Author Archives: Niklas

Mule 4: Set the output encoding on a flat file transformation

Every now and then you are working on a UTF-8 system and you have to deliver a flat file (or any other string) to a ISO-8859-1 system, as an example. To achive this we need to tell Mule that we want the result in ISO-8859-1 and not the default UTF-8.

This can be solved like this:

%dw 2.0
output application/flatfile encoding="ISO-8859-1"

Without the “encoding=ISO-8859-1” we would get the default UTF-8 encoding. This works of course for many other encoding-transformations

Tested on Mule 4.3, Anypoint Studio 7.11 on Windows 10

Mule flat file mapping: “com.mulesoft.flatfile.lexical.WriteException – effective length * is greater than * for component…” – alternative solution

This falls under the category “strange error messages” in Mule. Normaly you would think that the data you send is bigger than the longest allowed value, but in my case it was because I was trying to map a value from an array without setting a index

Tested in Anypoint Studio 7.11.1 and Mule 4.3

Testing a simple web app in Mule with JUnit

A simple JUnit test for when you just can’t remember the Mule JUnit testing syntax 😉

Mule flow
A Mule flow that takes a username as input and concatenates it with a “Hello”. The request is done by a GET call:

Flow code



    <http:listener-config 
       name="HTTP_Listener_Configuration" 
       host="0.0.0.0" 
       port="8081" 
       doc:name="HTTP Listener Configuration">
    
        <http:listener 
           config-ref="HTTP_Listener_Configuration" 
           path="/test" 
           doc:name="HTTP">
        <set-payload 
           value="#['Hello '+message.inboundProperties.'http.query.params'.username]" 
           doc:name="Set Payload">
    

Java test code:

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

import junit.framework.Assert;

public class MyTests extends FunctionalTestCase {
	
	@Test
	public void testMyFlow() throws Exception {
        // Get the Mule Context
		MuleClient muleClient = muleContext.getClient();
		
        // Create an empty test Mule message
		DefaultMuleMessage message = 
                        new DefaultMuleMessage(null, muleContext);
		
        // Make a call to the tested web service
		MuleMessage responseMessage = muleClient.send(
                       "http://localhost:8081/test?username=Niklas", 
                       message
                    );
		
        // Get response as a string
		String responsePayload = responseMessage.getPayloadAsString();
		
        // Assert response
		Assert.assertEquals("Hello Niklas", responsePayload);
		
	}
	
	@Override
	protected String[] getConfigFiles() {
         // Path to the Mule configuration file that is being tested
		return new String[]{"src/main/app/helloworld.xml"};
	}

}

Tested on OSX 10.15.7, Mule 3.9.4, and JUnit 4.13