Warning: A non-numeric value encountered in /home/ricston2/public_html/blogarchive/wp-content/themes/Divi/functions.php on line 5766

I started writing about this Mule transformer in a previous post. Before tackling the actual transformer code, I wanted to generate the necessary test case for it. There are two test cases that I need:

1. A test case for the transformer itself. Don’t forget that the transformer is nothing more than a Java class and therefore a standard test case is needed.
2. A test case for a Mule configuration. I should use the transformer inside a Mule config before I can be sure that it works well, so I will need a Mule test case too.

In this blog post, I’m going to show the Mule test case:

public class MessageAttachmentTransformerTestCase extends
  FunctionalTestCase {

This is the declaration of the test case. Note that we’re not inheriting from the usual TestCase class but from Mule’s FunctionalTestCase class instead. This class knows how to initialise a Mule server for us and means we don’t need to do it ourselves. To do this, however, it needs to know which configuration file you intend to use. The FunctionalTestCase class has a method called getConfigResources() that must be implemented which should return the path to the config file:

@Override
protected String getConfigResources() {
  return "mule-config.xml";
}

As you can see here, this method merely returns the name of the configuration file.

In our test case, we’re going to use the MuleClient to communicate with the Mule server and will need to do the following:

1. Send a message to our Mule server. Our Mule server should be configured to use the new transformer and output the transformed message on a different endpoint.
2. Read a message from our Mule server. This operation will, obviously, use the correct outbound endpoint.
3. Assert that the message received from Mule is the same one sent in and that it also has an attachment.

The code for this test case is as follows:

public void TestTransformer () {
  MuleClient myClient;
  MuleMessage response = null;
  try {
    myClient = new MuleClient ();
    myClient.dispatch ("vm://FromTestCase",
      new DefaultMuleMessage ("Test Payload"));
    response = myClient.request("vm://ToTestCase", 5000);
  } catch (MuleException e) {
    fail (e.getDetailedMessage());
  }

  assertNotNull (response);
  assertNotNull (response.getPayload());
  assertEquals (((String) response.getPayload()), "Test Payload");
  assertTrue (response.getAttachmentNames().size() != 0);
}