2 回答

TA貢獻(xiàn)1777條經(jīng)驗(yàn) 獲得超10個(gè)贊
您需要在您的情況下使用json()而不是string()因?yàn)槟捻憫?yīng)在內(nèi)部轉(zhuǎn)換為 JSON。并使用ObjectMapperfromcom.fasterxml.jackson.databind.ObjectMapper將您的響應(yīng)列表轉(zhuǎn)換為 JSON。
@Test
public void getHello() throws Exception {
ObjectMapper mapper = new ObjectMapper();
String result = mapper.writeValueAsString(new UserController().getUsers());
mvc.perform(MockMvcRequestBuilders.get("/auth").accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().json(result));
}
編輯:您可以使用 ObjectMapper 將對(duì)象列表轉(zhuǎn)換為 JSON。

TA貢獻(xiàn)1776條經(jīng)驗(yàn) 獲得超12個(gè)贊
您正在嘗試將內(nèi)容字符串與實(shí)際對(duì)象進(jìn)行比較,就像toString()調(diào)用其方法時(shí)的情況一樣,如下所示:
Expected: <[UserDTO [firstName=Ahmad, lastName=shahzad, userName=1, emailAddress=ahmad.shahzad@null], UserDTO [firstName=Jamshaid, lastName=iqbal, userName=2, emailAddress=jamshaid.ali@null], UserDTO [firstName=Waqas, lastName=Akram, userName=3, emailAddress=waqas.akram@null]]>
but: was "[{\"firstName\":\"Ahmad\",\"lastName\":\"shahzad\",\"userName\":\"1\",\"emailAddress\":\"ahmad.shahzad@null\"},{\"firstName\":\"Jamshaid\",\"lastName\":\"iqbal\",\"userName\":\"2\",\"emailAddress\":\"jamshaid.ali@null\"},{\"firstName\":\"Waqas\",\"lastName\":\"Akram\",\"userName\":\"3\",\"emailAddress\":\"waqas.akram@null\"}]"
您必須解析內(nèi)容中的每個(gè)屬性或使用某種 JSON 解析器來批量解析。然后將每個(gè)解析的屬性與 User 對(duì)象的字段進(jìn)行比較。
添加回答
舉報(bào)