【问题标题】:Mule - JUnit Send requests - Best practiceMule - JUnit 发送请求 - 最佳实践
【发布时间】:2016-03-22 22:55:27
【问题描述】:

这是我用于测试的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

然后,当我使用配置资源测试流程时,我有一个主类:

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 个类对其进行测试。

与其他课程一起测试的最佳实践是什么?

【问题讨论】:

  • 只是一个不相关的评论:作为最佳实践,您应该从 MuleContext 中获取 MuleClient 而不是自己创建。那是muleContext.getClient()

标签: java unit-testing junit mule


【解决方案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());
    }



}

是的,从 context 获取 MuleClient 效率更高:

MuleClient client = muleContext.getClient();

【讨论】:

  • 谢谢!这是一个很好的解决方案,但如果我将client.sendmuleContext.getClient() 一起使用,它会返回错误The method send(String, MuleMessage) in the type MuleClient is not applicable for the arguments (String, String) 如何使用字符串创建MuleMessage?
  • send(String url, Object payload, Map<String, Object> messageProperties) 将第三个参数作为空值传递
  • 已弃用,我可以使用的唯一方法是:send(String url, MuleMessage message)
猜你喜欢
  • 2021-01-02
  • 2021-05-07
  • 2020-11-20
  • 1970-01-01
  • 1970-01-01
  • 2018-02-22
  • 2020-04-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多