MockHttpServletResponse:检查xml内容

时间:2014-03-05 15:39:44

标签: xpath xml-namespaces spring-mvc-test

我正在使用MockMvc测试控制器。这就是响应的样子:

MockHttpServletResponse:
              Status = 200
       Error message = null
             Headers = {Content-Type=[text/xml]}
        Content type = text/xml
                Body = <?xml version="1.0" encoding="UTF-8" standalone="yes"?><ns2:diagnosisCode xmlns:ns2="http://schemas.mycompany.co.za/health" effectiveStartDate="2014-03-05T00:00:00+02:00" effectiveEndDate="2014-03-05T23:59:59.999+02:00" diagnosisId="1"><diagnosisCodeId><codingSchemaCode>irrelevant schema</codingSchemaCode><diagnosisCode>irrelevant code</diagnosisCode></diagnosisCodeId></ns2:diagnosisCode>
       Forwarded URL = null
      Redirected URL = null
             Cookies = []

Body行的漂亮印刷版:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<ns2:diagnosisCode xmlns:ns2="http://schemas.mycompany.co.za/health" effectiveStartDate="2014-03-05T00:00:00+02:00" effectiveEndDate="2014-03-05T23:59:59.999+02:00" diagnosisId="1">
    <diagnosisCodeId>
        <codingSchemaCode>irrelevant schema</codingSchemaCode>
        <diagnosisCode>irrelevant code</diagnosisCode>
    </diagnosisCodeId>
</ns2:diagnosisCode>

MockMvc上的通话看起来像是

mockMvc.perform(
        get("/diagnostic/diagnosisCodes/{schema}/{code}", IRRELEVANT_SCHEMA, IRRELEVANT_CODE).accept(MediaType.TEXT_XML))
        .andDo(print())
        .andExpect(content().contentType(MediaType.TEXT_XML))
        .andExpect(status().isOk())
        .andExpect(xpath("diagnosisCodeId/diagnosisCode").string(IRRELEVANT_CODE))
        .andExpect(xpath("diagnosisCodeId/codingSchemaCode").string(IRRELEVANT_SCHEMA));

我很确定我误解了我应该如何在这里使用XPath,但为什么这个断言失败了?我的期望应该是什么样的?

java.lang.AssertionError: XPath diagnosisCode expected:<irrelevant code> but was:<>

2 个答案:

答案 0 :(得分:1)

我不完全确定XPath上下文是什么(或者它是否是文档节点),但我看到两个可能的问题并猜测两者都适用:

  • 您尝试匹配作为根元素的< diagnosisCodeId/>元素。没有,但他们是<diagnosisCode>的孩子。可以包括根节点的轴步骤(可能更好的方式),也可以在查询前使用descendant-or-self轴步//

    /diagnosisCode/diagnosisCodeId/diagnosisCode
    //diagnosisCodeId/diagnosisCode
    
  • 该文档使用名称空间(对于根元素)。除了上面描述的根元素问题之外,要么注册该命名空间(更好的解决方案,但我不知道如何在spring MVC中执行此操作)或使用以下解决方法忽略它:

    /*[local-name() = 'diagnosisCode']/diagnosisCodeId/diagnosisCode
    

    首先匹配所有子节点,然后限制具有适当元素名称的节点(忽略名称空间)。

    通过添加XPath 2.0支持(例如by including Saxon as library),您还可以使用通配符命名空间匹配器:

    /*:diagnosisCode/diagnosisCodeId/diagnosisCode
    

    如果您将名称空间URI http://schemas.mycompany.co.za/health注册为ns2,则查询将如下所示

    /ns2:diagnosisCode/diagnosisCodeId/diagnosisCode
    

答案 1 :(得分:0)

xpath的重载需要使用Map<String, String>的命名空间:

Map<String, String> ns = Map.of("ns2", "http://schemas.mycompany.co.za/health");
mockMvc.perform(get("/diagnostic/diagnosisCodes/{schema}/{code}", IRRELEVANT_SCHEMA, IRRELEVANT_CODE)
        .accept(MediaType.TEXT_XML))
        .andExpect(xpath("ns2:diagnosisCodeId/diagnosisCode", ns).string(IRRELEVANT_CODE))
        .andExpect(xpath("ns2:diagnosisCodeId/codingSchemaCode", ns).string(IRRELEVANT_SCHEMA));