title: How to test if JSON path does not include a specific element, or if the element is present it is null?
body: <p>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 object does not include the corresponding json element. So when my unit test routine tries to test if this is null (which is the expected behavior for my case, I don't want the password to be available in json object), test routine runs into an exception:</p>
  
  <blockquote>
    <p>java.lang.AssertionError: No value for JSON path: $.password, exception: No results for path: $['password']</p>
  </blockquote>
  
  <p>This is the unit test method I wrote, testing the 'password' field with is(nullValue()) method:</p>
  
  <pre><code>@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());
  }
  </code></pre>
  
  <p>I have also tried it with jsonPath().exists() which gets similar exception stating that the path doesn't exist. I am sharing some more code snippets so that the whole situation becomes more readable.</p>
  
  <p>The controller method I am testing looks something like this:</p>
  
  <pre><code>@RequestMapping(value="/users/{userId}", method= RequestMethod.GET)
  public ResponseEntity&lt;UserResource&gt; 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&lt;&gt;(userResource, HttpStatus.OK);
      } else {
          return new ResponseEntity&lt;&gt;(HttpStatus.NOT_FOUND);
      }
  }
  </code></pre>
  
  <p>I am using spring hateos resource assembler for converting entity to resource objects and this is my resource class:</p>
  
  <pre><code>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;
      }
  }
  </code></pre>
  
  <p>I understand why this is getting an exception, also in a way, the test is successful that it could not find the password field. But what I want to do is, run this test to ensure that the field is not present, or if present, it contains null value. How can I achieve this?</p>
  
  <p>There is a similar post in stack overflow:
  <a href="https://stackoverflow.com/questions/25318424/hamcrest-with-mockmvc-check-that-key-exists-but-value-may-be-null">Hamcrest with MockMvc: check that key exists but value may be null</a></p>
  
  <p>In my case, the field may be non existent as well.</p>
  
  <p>For the record, these are the versions of test packages I am using:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-core&lt;/artifactId&gt;
          &lt;version&gt;2.6.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-annotations&lt;/artifactId&gt;
          &lt;version&gt;2.6.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.fasterxml.jackson.core&lt;/groupId&gt;
          &lt;artifactId&gt;jackson-databind&lt;/artifactId&gt;
          &lt;version&gt;2.6.1&lt;/version&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
          &lt;artifactId&gt;json-path&lt;/artifactId&gt;
          &lt;version&gt;2.0.0&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
          &lt;artifactId&gt;json-path-assert&lt;/artifactId&gt;
          &lt;version&gt;2.0.0&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;junit&lt;/groupId&gt;
          &lt;artifactId&gt;junit&lt;/artifactId&gt;
          &lt;version&gt;4.12&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;org.mockito&lt;/groupId&gt;
          &lt;artifactId&gt;mockito-all&lt;/artifactId&gt;
          &lt;version&gt;1.10.19&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>Thanks in advance.</p>
  
  <p>[EDIT]
  To be more precise, say, you have to write a test for an entity where you know some of the fields need to be null or empty or should not even exists, and you don't actually go through the code to see if there is a JsonIgnore added on top of the property. And you want your tests to pass, how can I do this.</p>
  
  <p>Please feel free to tell me that this is not practical at all, but still would be nice to know.</p>
  
  <p>[EDIT]
  The above test succeeds with the following older json-path dependencies:</p>
  
  <pre><code>    &lt;dependency&gt;
          &lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
          &lt;artifactId&gt;json-path&lt;/artifactId&gt;
          &lt;version&gt;0.9.1&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
      &lt;dependency&gt;
          &lt;groupId&gt;com.jayway.jsonpath&lt;/groupId&gt;
          &lt;artifactId&gt;json-path-assert&lt;/artifactId&gt;
          &lt;version&gt;0.9.1&lt;/version&gt;
          &lt;scope&gt;test&lt;/scope&gt;
      &lt;/dependency&gt;
  </code></pre>
  
  <p>[EDIT] Found a quickfix that works with latest version of jayway.jasonpath after reading the documentation of spring's json path matcher.</p>
  
  <pre><code>.andExpect(jsonPath("$.password").doesNotExist())
  </code></pre>