我一直在为一个简单的 Spring Web 应用程序编写一些简单的单元测试例程。当我在资源的 getter 方法上添加 @JsonIgnore 注释时,生成的 json 对象不包含相应的 json 元素。因此,当我的单元测试例程尝试测试这是否为空(这是我的情况的预期行为,我不希望密码在 json 对象中可用)时,测试例程会遇到异常:
java.lang.AssertionError:JSON 路径没有值:$.password,异常:路径没有结果:$['password']
这是我写的单元测试方法,用 is(nullValue()) 方法测试“密码”字段:
@Test
public void getUserThatExists() throws Exception {
User user = new User();
user.setId(1L);
user.setUsername("zobayer");
user.setPassword("123456");
when(userService.getUserById(1L)).thenReturn(user);
mockMvc.perform(get("/users/1"))
.andExpect(jsonPath("$.username", is(user.getUsername())))
.andExpect(jsonPath("$.password", is(nullValue())))
.andExpect(jsonPath("$.links[*].href", hasItem(endsWith("/users/1"))))
.andExpect(status().isOk())
.andDo(print());
}
我也尝试过使用 jsonPath().exists() 得到类似的异常,指出路径不存在。我正在分享更多代码片段,以便整个情况变得更具可读性。
我正在测试的控制器方法如下所示:
@RequestMapping(value="/users/{userId}", method= RequestMethod.GET)
public ResponseEntity<UserResource> getUser(@PathVariable Long userId) {
logger.info("Request arrived for getUser() with params {}", userId);
User user = userService.getUserById(userId);
if(user != null) {
UserResource userResource = new UserResourceAsm().toResource(user);
return new ResponseEntity<>(userResource, HttpStatus.OK);
} else {
return new ResponseEntity<>(HttpStatus.NOT_FOUND);
}
}
我正在使用 spring hatos 资源组装器将实体转换为资源对象,这是我的资源类:
public class UserResource extends ResourceSupport {
private Long userId;
private String username;
private String password;
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
@JsonIgnore
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
我理解为什么这会出现异常,在某种程度上,测试是成功的,它找不到密码字段。但我想做的是,运行这个测试以确保该字段不存在,或者如果存在,它包含空值。我怎样才能做到这一点?
堆栈溢出中有一个类似的帖子: Hamcrest with MockMvc: check that key exists but value may be null
就我而言,该字段也可能不存在。
作为记录,这些是我正在使用的测试包的版本:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>2.0.0</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-all</artifactId>
<version>1.10.19</version>
<scope>test</scope>
</dependency>
提前致谢。
[编辑] 更准确地说,你必须为一个实体编写一个测试,你知道其中一些字段需要为空或空或者甚至不应该存在,并且你实际上并没有通过代码来查看如果在属性顶部添加了 JsonIgnore。你希望你的测试通过,我该怎么做。
请随时告诉我,这根本不实用,但仍然很高兴知道。
[编辑] 上述测试通过以下较旧的 json-path 依赖项成功:
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path</artifactId>
<version>0.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.jayway.jsonpath</groupId>
<artifactId>json-path-assert</artifactId>
<version>0.9.1</version>
<scope>test</scope>
</dependency>
[编辑] 在阅读了 spring 的 json 路径匹配器的文档后,找到了一个适用于最新版本 jayway.jasonpath 的快速修复。
.andExpect(jsonPath("$.password").doesNotExist())