我有以下XML字符串, 如何編寫XPath來獲取價值「休息/賬戶/ 123」如何編寫JUnit測試案例的XPath
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<account xmlns:atom="http://www.w3.org/2005/Atom">
<atom:link rel="self" href="http://localhost/rest/accounts/123"/>
<name>satya</name>
<password>123</password>
</account>
我想寫JUnit測試用例
mockMvc.perform(get("/rest/accounts/123")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(xpath("/account/atom:link/href()")
.string(containsString("/rest/accounts/123")))
.andExpect(xpath("/account/name/text()").string(equalTo("satya")));
但線
MockMvcResultMatchers.xpath。( 「/帳戶/原子:鏈路/ HREF()」)的字符串( 匹配ers.containsString(「/ rest/accounts/123」)))
出現錯誤。
按照建議我改變代碼
mockMvc.perform(
MockMvcRequestBuilders.get("/rest/accounts/123").contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_XML))
.andDo(print())
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(
MockMvcResultMatchers.xpath("/account/atom:link/@href,").string(
Matchers.containsString("/rest/accounts/123")))
.andExpect(MockMvcResultMatchers.xpath("/account/name/text()").string(Matchers.equalTo("satya")));
// .andExpect(MockMvcResultMatchers.jsonPath("$.links[*].href").exists());
}
JUnit測試案例給錯誤
com.sun.org.apache.xpath.internal.domapi.XPathStylesheetDOM3Exception: 前綴必須解析命名空間:原子
'HREF()'應該是'@ href' – trincot
我在JUnit改變代碼,但得到錯誤運行 – Satya