2012-11-21 34 views
1

有沒有方法比較Camel Junit中的XML消息?Camel Junit中的XML消息比較

我使用下面的代碼:

@RunWith(CamelSpringJUnit4ClassRunner.class) 
@ContextConfiguration(locations = { "classpath:camel-context-test.xml" }) 
@DirtiesContext(classMode = ClassMode.AFTER_EACH_TEST_METHOD) 
@MockEndpoints("*") 
public class CamelRoutesTest/* extends XMLTestCase */{ 
    private static final Log LOG = LogFactory.getLog(CamelRoutesTest.class); 
    @Autowired 
    protected CamelContext camelContext; 

    @EndpointInject(uri = "mock:d2") 
    protected MockEndpoint direct1; 

    @Produce(uri = "direct:d1") 
    protected ProducerTemplate d1; 

    @Test 
    public void test1() throws Exception { 
     LOG.info("Starting testTradeSaveToPL test"); 

      //node1 comes BEFORE node2 
    String sendMsg = "<test><node1>1</node1><node2>2</node2></test>"; 

      //node1 comes AFTER node2 
    String valMsg1 = "<test><node2>2</node2><node1>1</node1></test>"; 


     direct1.expectedBodiesReceivedInAnyOrder(valMsg1); 

     d1.sendBody(sendMsg); 
     direct1.assertIsSatisfied(camelContext); 
    } 
} 

我的問題是,XML消息我送的路線,節點1,而在回覆節點2節點1來之前談到節點2之前。

通過查看我知道兩個XML都是平等的,但由於代碼沒有字符串比較,它失敗了。

我知道XMLJUnit比較工具,但我怎麼能將它集成到給定的測試用例?

回答

2

我在我的駱駝單元測試中集成了XMLUnit以比較XML消息。

在你的構造,設置XMLUnit測試:

@Override 
public void setUp() throws Exception { 
    super.setUp(); 

    //Tell XML Unit to ignore whitespace between elements and within elements 
    XMLUnit.setIgnoreWhitespace(true); 
    XMLUnit.setNormalizeWhitespace(true); 
} 

再後來就可以運行一個斷言:

Diff myDiff = new Diff(actualResponse, expectedResponseAsString); 
    assertTrue("XML identical " + myDiff.toString(), 
        myDiff.identical()); 

您可以使用此依賴性:

<dependency> 
     <groupId>xmlunit</groupId> 
     <artifactId>xmlunit</artifactId> 
     <version>1.3</version> 
     <scope>test</scope> 
    </dependency> 

這裏給用戶的鏈接指南:

http://xmlunit.sourceforge.net/userguide/html/index.html

由於元素序列實際上是不同的,所以這個測試框架可能無法幫到你。但是,您也可以只使用Java或JDOM中的XPath API來運行您的斷言。

謝謝, Yogesh