【问题标题】:How to use camel mock to get full XML?如何使用骆驼模拟来获取完整的 XML?
【发布时间】:2015-09-08 11:59:35
【问题描述】:

在骆驼单元测试中,我可以使用 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();
  }

【问题讨论】:

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


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

    【讨论】:

    • mock.getExchanges().get(0).getIn().getBody(String.class);就足够了:-)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多