【发布时间】:2013-08-30 01:37:36
【问题描述】:
我正在测试一个 Spring MVC 应用程序。
我需要确保对浏览器的响应具有某种模式,我的应用程序会发回一些 json,并且对于每个 Json 项目,我都有一个名为 DT_RowId 的字段不想比较,因为 DT_RowId 包含一个随机数。因此,我想比较所有 Json 主体,除了 DT_RowId 及其内容。
顺便说一句,DT_RowId 的典型出现是“DT_RowId”:“8407709537703772”。一个典型的 json 响应是:
{"id":-1,"fieldErrors":[],"aaData":[{"id":8002,"firstname":"Bob","lastname":"Jones","email":"bob.jones@gmail.com","DT_RowId":"8407709537703772"},{"id":8002,"firstname:"Dan","lastname":"Jones","email":"dan.jones@gmail.com","DT_RowId":"8404309537701754"}]}
下面是我的测试:
@Test
public void testGetUsersJson() throws Exception {
mockMvc.perform(get("/users")
.accept(MediaType.APPLICATION_JSON))
.andDo(print())
.andExpect(content().contentType("application/json"))
// How can I modify the line below??
.andExpect(content().bytes(IOUtils.toByteArray(ctx.getResource("classpath:responses/users.getUsersJson.mywebapp.response.json").getInputStream())))
.andExpect(status().isOk())
.andExpect(redirectedUrl(null))
.andExpect(forwardedUrl(null));
}
如何修改上面的代码?我想比较除字段“DT_RowId”之外的所有 Json 响应。有什么聪明的主意吗?
【问题讨论】:
-
您需要
DT_RowId字段吗?如果没有,为什么不在 JSON 响应中省略它? -
是的,我需要 DT_RowId 字段,否则这个问题没有任何意义
-
这只是一个问题。如果您根本没有在客户端中使用 DT_RowId,那么省略它会使比较更容易。有时人们看不到明显的东西,因为他们看错了地方。
标签: java json spring spring-mvc junit