第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

java.lang.AssertionError: 響應(yīng)內(nèi)容

java.lang.AssertionError: 響應(yīng)內(nèi)容

萬千封印 2023-05-10 14:06:13
我正在嘗試測(cè)試 aUnitTest但java.lang.AssertionError: Response content在運(yùn)行測(cè)試時(shí)得到了。我的日志java.lang.AssertionError: Response contentExpected: <[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\"}]"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]]>Actual   :"[{\"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\"}]"at org.hamcrest.MatcherAssert.assertThat(MatcherAssert.java:20)at org.springframework.test.web.servlet.result.ContentResultMatchers.lambda$string$3(ContentResultMatchers.java:130)at org.springframework.test.web.servlet.MockMvc$1.andExpect(MockMvc.java:195)單元測(cè)試用例類@RunWith(SpringRunner.class)@SpringBootTest@AutoConfigureMockMvcpublic class UnitTestAuth {@Autowiredpublic MockMvc mvc;@Testpublic void getHello() throws Exception {    mvc.perform(MockMvcRequestBuilders.get("/auth").accept(MediaType.APPLICATION_JSON))            .andExpect(status().isOk())            .andExpect(content().string(equalTo(new UserController().getUsers())));}}
查看完整描述

2 回答

?
不負(fù)相思意

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。


查看完整回答
反對(duì) 回復(fù) 2023-05-10
?
叮當(dāng)貓咪

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)行比較。


查看完整回答
反對(duì) 回復(fù) 2023-05-10
  • 2 回答
  • 0 關(guān)注
  • 308 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)