0

In a unit test with camel, I can make asserts using xpath to check if the output xml is correct. But instead, I'd like to use XMLUnit to validate the xml against another entire xml file. Is that possible? The following test succeeds, but I'd like to adjust it to get the actual 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();
  }
4

1 に答える 1

2

これは、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());
    }
}
于 2015-09-08T13:03:14.893 に答える