Mule - JUnit发送请求 - 最佳实践

时间:2016-03-22 12:44:22

标签: java unit-testing junit mule

这是我用于测试的mule flow

HTTP Listener > Logger (Message) > HTTP Request POST > Logger (Response)

在我的 mule项目中,我有5个classes。这是一个例子:

@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...]
}

我将元素@XmlRootElement设为class,将@XmlElement设为setters

然后,当我使用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());
    }
}

但我想用剩下的4个课程来测试它。

使用其他类测试的最佳做法是什么?

1 个答案:

答案 0 :(得分:1)

这样的事情怎么样:

    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());
    }



}

是的,从MuleClient获取context会稍微提高效率:

MuleClient client = muleContext.getClient();