为了账号安全,请及时绑定邮箱和手机立即绑定

java.lang.AssertionError: 响应内容

java.lang.AssertionError: 响应内容

万千封印 2023-05-10 14:06:13
我正在尝试测试 aUnitTest但java.lang.AssertionError: Response content在运行测试时得到了。我的日志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)单元测试用例类@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 回答

?
不负相思意

TA贡献1777条经验 获得超10个赞

您需要在您的情况下使用json()而不是string()因为您的响应在内部转换为 JSON。并使用ObjectMapperfromcom.fasterxml.jackson.databind.ObjectMapper将您的响应列表转换为 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 将对象列表转换为 JSON。


查看完整回答
反对 回复 2023-05-10
?
叮当猫咪

TA贡献1776条经验 获得超12个赞

您正在尝试将内容字符串与实际对象进行比较,就像toString()调用其方法时的情况一样,如下所示:


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\"}]"

您必须解析内容中的每个属性或使用某种 JSON 解析器来批量解析。然后将每个解析的属性与 User 对象的字段进行比较。


查看完整回答
反对 回复 2023-05-10
  • 2 回答
  • 0 关注
  • 241 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信