Reputation: 1289
This is the mule flow
that I use to the tests:
HTTP Listener > Logger (Message) > HTTP Request POST > Logger (Response)
In my mule project I have 5 classes
. This is an example:
@XmlRootElement
public class Car {
String name;
String color;
public Car() {
super();
}
public Car(String name, String color) {
super();
this.name = name;
this.color = color;
}
public String getName() {
return name;
}
@XmlElement
public void setName(String name) {
this.name = name;
}
[More Setters and Getters...]
}
I asign the element @XmlRootElement
to class
and @XmlElement
to setters
.
Then, I have a main class when I test the flow with Config Resources:
public class JUnitSend extends FunctionalTestCase {
@Override
protected String getConfigResources() {
return "send-xml.xml";
}
public String getName() {
return "Mule Server Test";
}
public Car myCar()
{
Car myCar = new Car();
myCar.setName("Ferrari");
myCar.setColor("Red");
return myCar;
}
@Test
public void sendXML() throws Exception {
JAXBContext jaxbContext = JAXBContext.newInstance(Car.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(myCar(), sw);
String xmlString = sw.toString();
MuleClient client = new MuleClient(muleContext);
MuleMessage result = client.send("http://localhost:8090/", xmlString, null);
assertEquals("Hello", result.getPayloadAsString());
}
}
But I want to test it with the remaining 4 classes.
What is the best practises to test with the other classes?
Upvotes: 0
Views: 303
Reputation: 4551
How about something like this:
public class JUnitSend extends FunctionalTestCase {
@Override
protected String getConfigResources() {
return "send-xml.xml";
}
public String getName() {
return "Mule Server Test";
}
public Car myCar()
{
Car myCar = new Car();
myCar.setName("Ferrari");
myCar.setColor("Red");
return myCar;
}
private String marshallObject(Object object)
{
JAXBContext jaxbContext = JAXBContext.newInstance(Car.class);
Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
StringWriter sw = new StringWriter();
jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
jaxbMarshaller.marshal(object, sw);
return sw.toString();
}
@Test
public void testSendXml_car() throws Exception {
MuleClient client = new MuleClient(muleContext);
MuleMessage result = client.send("http://localhost:8090/", marshallObject(myCar(), null);
assertEquals("Hello", result.getPayloadAsString());
}
@Test
public void testSendXml_otherObject() throws Exception {
MuleClient client = new MuleClient(muleContext);
MuleMessage result = client.send("http://localhost:8090/", marshallObject(myOtherObject(), null);
assertEquals("Hello", result.getPayloadAsString());
}
}
And yes getting MuleClient
from context
is slightly more efficient:
MuleClient client = muleContext.getClient();
Upvotes: 1