java.lang.AssertionError: 响应内容

我正在尝试测试 aUnitTest但java.lang.AssertionError: Response content

在运行测试时得到了。

我的日志


java.lang.AssertionError: Response content

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

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

@AutoConfigureMockMvc

public class UnitTestAuth {

@Autowired

public MockMvc mvc;


@Test

public void getHello() throws Exception {

    mvc.perform(MockMvcRequestBuilders.get("/auth").accept(MediaType.APPLICATION_JSON))

            .andExpect(status().isOk())

            .andExpect(content().string(equalTo(new UserController().getUsers())));

}



}

万千封印
浏览 242回答 2
2回答

不负相思意

您需要在您的情况下使用json()而不是string()因为您的响应在内部转换为 JSON。并使用ObjectMapperfromcom.fasterxml.jackson.databind.ObjectMapper将您的响应列表转换为 JSON。@Testpublic void getHello() throws Exception {ObjectMapper mapper = new ObjectMapper();String result = mapper.writeValueAsString(new UserController().getUsers());&nbsp; &nbsp; mvc.perform(MockMvcRequestBuilders.get("/auth").accept(MediaType.APPLICATION_JSON))&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .andExpect(status().isOk())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .andExpect(content().json(result));}编辑:您可以使用 ObjectMapper 将对象列表转换为 JSON。

叮当猫咪

您正在尝试将内容字符串与实际对象进行比较,就像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]]>&nbsp;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 对象的字段进行比较。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java