How to test if JSON path does not include a specific element, or if the element is present it is null?

前端 未结 5 1308
野趣味
野趣味 2021-02-04 23:17

I have been writing some simple unit testing routines for a simple spring web application. When I add @JsonIgnore annotation on a getter method of a resource, the resulting json

5条回答
  •  无人及你
    2021-02-04 23:43

    There is a difference between the property that is present, but having null value, and the property not being present at all.

    If the test should fail only when there is a non-null value, use:

    .andExpect(jsonPath("password").doesNotExist())
    

    If the test should fail as soon as the property is present, even with a null value, use:

    .andExpect(jsonPath("password").doesNotHaveJsonPath())
    

提交回复
热议问题