如何使用camel mock获取完整的XML?

时间:2015-09-08 11:59:36

标签: unit-testing junit mocking apache-camel xmlunit

在使用camel的单元测试中,我可以使用xpath生成断言来检查输出xml是否正确。但相反,我想使用XMLUnit来验证xml与另一个完整的xml文件。那可能吗?以下测试成功,但我想调整它以获得实际的XML。

  @Test
  public void testSupplierSwitch() throws Exception
  { 
    MockEndpoint mock = getMockEndpoint("mock:market-out");
    mock.expectedMessageCount(1);

    EdielBean edielBean = (EdielBean)context.getBean("edielbean");
    edielBean.startSupplierSwitch(createCustomer(), createOrder(), "54", "43");

    assertMockEndpointsSatisfied();
  }

1 个答案:

答案 0 :(得分:1)

以下是使用mockEndpoint.getExchanges()

解决问题的一个示例
public class XmlUnitTest extends CamelTestSupport{

    @EndpointInject(uri = "mock:market-out")
    MockEndpoint marketOut;

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
        context.addRoutes(new RouteBuilder() {
            @Override
            public void configure() throws Exception {
                from("direct:in")
                    .setBody(constant("<xml>data</xml>"))
                    .to(marketOut.getEndpointUri());
            }
        });


    }

    @Test
    public void sameXml() throws Exception {
        marketOut.expectedMessageCount(1);
        template.sendBody("direct:in", "body");
        marketOut.assertIsSatisfied();
        final String actual = marketOut.getExchanges().get(0).getIn().getBody(String.class);
        final Diff diff = XMLUnit.compareXML("<xml>data</xml>", actual);
        assertTrue(diff.similar());
        assertTrue(diff.identical());
    }

    @Test()
    public void notSameXml() throws Exception {
        marketOut.expectedMessageCount(1);
        template.sendBody("direct:in", "body");
        marketOut.assertIsSatisfied();
        final String actual = marketOut.getExchanges().get(0).getIn().getBody(String.class);

        final Diff diff = XMLUnit.compareXML("<xml>invalid</xml>", actual);
        assertFalse(diff.similar());
        assertFalse(diff.identical());
    }
}