【问题标题】:How to disable JsonProperty or JsonIgnore on test如何在测试中禁用 JsonProperty 或 JsonIgnore
【发布时间】:2017-02-26 18:36:52
【问题描述】:

如果有办法在测试中禁用@JsonProperty(access = JsonProperty.Access.WRITE_ONLY)@JsonIgnore

我正在尝试测试我的createUser(),但我需要在解析我的User 对象时启用user.getPassword() 方法。

如果我评论 @JsonProperty 行它可以工作,但如果我这样做,密码字段将在 GET /users or GET /users/{id} 方法上返回。

这是我的测试

@Test
public void createUser() throws Exception {
    User user = UserFactory.newUser();
    String userJson = objectMapper.writeValueAsString(user);

    LOGGER.info("User to register: " + userJson);

    mockMvc.perform(post("/users")
            .content(userJson)
            .contentType(contentType))
            .andExpect(status().isCreated())
            .andExpect(jsonPath("$.id", is(notNullValue())));
}

创建新用户的方法:

public static User newUser() {
    Fairy fairy = Fairy.create();
    Person person = fairy.person();

    User user = new User();
    user.setName(person.getFirstName());
    user.setLastName(person.getLastName());
    user.setEmail(person.getEmail());
    user.setUsername(person.getUsername());
    user.setPassword(person.getPassword());
    user.setSex(person.isMale() ? User.Sex.MALE : User.Sex.FEMALE);
    user.setPhone(person.getTelephoneNumber());
    user.setCountry(person.getAddress().getCity());

    return user;
}

这是用ObjectMapper序列化User对象后得到的json:

{
    "createdAt" : null,
    "updatedAt" : null,
    "name" : "Jasmine",
    "lastName" : "Neal",
    "email" : "jasmineneal@yahoo.com",
    "username" : "jasminen",
    "sex" : "FEMALE",
    "phone" : "321-104-989",
    "country" : "San Francisco"
}

UserController.class 方法

@RequestMapping(method = RequestMethod.POST)
public ResponseEntity store(@Valid @RequestBody User user) {
    userService.store(user);
    return new ResponseEntity<Object>(user, HttpStatus.CREATED);
}

User.class 属性

@Column(name = "password", length = 100)
@NotNull(message = "error.password.notnull")
@JsonProperty(access = JsonProperty.Access.WRITE_ONLY) // If I comment this, it works
private String password;

这是一种解决方法吗?

【问题讨论】:

  • 我建议不要在您的测试中重复使用 User 模型,其唯一目的是免费进行 JSON 序列化。这样,您的 User 模型与您的测试紧密耦合。如果您的 JSON 映射中碰巧有错误,您将很难检测到它
  • 感谢您的建议,非常感谢。你建议我做什么?也许是一个处理这个请求然后将其解析为User 实例的类?
  • 至少,我会为您的测试创建一个单独的UserModel,以确保对原始User 的更改不会影响您的测试输入数据的处理方式。在此模型中,您可以简单地删除 WRITE_ONLY 标志。或者,您可以简单地通过一个简单的地图作为主体来执行您的请求

标签: spring-boot jackson jackson-modules


【解决方案1】:

您可以通过添加以下行来禁用所有 Jackson 注释:

objectMapper.disable(MapperFeature.USE_ANNOTATIONS);

更多信息,您可以查看link

在你的情况下,这应该有效:

@Test
public void createUser() throws Exception {
User user = UserFactory.newUser();
objectMapper.disable(MapperFeature.USE_ANNOTATIONS);
String userJson = objectMapper.writeValueAsString(user);

LOGGER.info("User to register: " + userJson);

mockMvc.perform(post("/users")
        .content(userJson)
        .contentType(contentType))
        .andExpect(status().isCreated())
        .andExpect(jsonPath("$.id", is(notNullValue())));
}

【讨论】:

    猜你喜欢
    • 2023-03-21
    • 1970-01-01
    • 2018-09-03
    • 2013-10-18
    • 2014-03-11
    • 2018-09-22
    • 2020-02-15
    • 2012-07-15
    • 2017-06-27
    相关资源
    最近更新 更多