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

前端 未结 5 1310
野趣味
野趣味 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:33

    I wanted to reuse the same code I use for testing for the parameter being supplied, and for it missing, and this is what I came up with

      @Test
      void testEditionFoundInRequest() throws JsonProcessingException {
        testEditionWithValue("myEdition");
      }
    
      @Test
      void testEditionNotFoundInRequest() {
        try {
          testEditionWithValue(null);
          throw new RuntimeException("Shouldn't pass");
        } catch (AssertionError | JsonProcessingException e) {
          var msg = e.getMessage();
          assertTrue(msg.contains("No value at JSON path"));
        }
      }
    
    
      void testEditionWithValue(String edition) {   
       var HOST ="fakeHost";
       var restTemplate = new RestTemplate();
       var myRestClientUsingRestTemplate = new MyRestClientUsingRestTemplate(HOST, restTemplate);
       MockRestServiceServer mockServer;
       ObjectMapper objectMapper = new ObjectMapper();
       String id = "userId";
       var mockResponse = "{}";
    
       var request = new MyRequest.Builder(id).edition(null).build();
       mockServer = MockRestServiceServer.bindTo(restTemplate).bufferContent().build();
    
       mockServer
            .expect(method(POST))
    
            // THIS IS THE LINE I'd like to say "NOT" found
            .andExpect(jsonPath("$.edition").value(edition))
            .andRespond(withSuccess(mockResponse, APPLICATION_JSON));
    
        var response = myRestClientUsingRestTemplate.makeRestCall(request);
      } catch (AssertionError | JsonProcessingException e) {
        var msg = e.getMessage();
        assertTrue(msg.contains("No value at JSON path"));
      }
    
    0 讨论(0)
  • 2021-02-04 23:39

    I had the same problem with the newer version. It looks to me that the doesNotExist() function will verify that the key is not in the result:

    .andExpect(jsonPath("$.password").doesNotExist())
    
    0 讨论(0)
  • 2021-02-04 23:42

    @JsonIgnore is behaving as expected, not producing the password in the json output, so how could you expect to test something that you are explicitly excluding from the output?

    The line:

    .andExpect(jsonPath("$.property", is("some value")));
    

    or even a test that the property is null:

    .andExpect(jsonPath("$.property").value(IsNull.nullValue()));
    

    correspond to a json like:

    {
    ...
    "property": "some value",
    ...
    }
    

    where the important part is the left side, that is the existence of "property":

    Instead, @JsonIgnore is not producing the porperty in the output at all, so you can't expect it not in the test nor in the production output. If you don't want the property in the output, it's fine, but you can't expect it in test. If you want it empty in output (both in prod and test) you want to create a static Mapper method in the middle that is not passing the value of the property to the json object:

    Mapper.mapPersonToRest(User user) {//exclude the password}
    

    and then your method would be:

    @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 = Mapper.mapPersonToRest(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);
        }
    }
    

    At this point, if your expectations are for Mapper.mapPersonToRest to return a user with a null password, you can write a normal Unit test on this method.

    P.S. Of course the password is crypted on the DB, right? ;)

    0 讨论(0)
  • 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())
    
    0 讨论(0)
  • 2021-02-04 23:51

    doesNotHaveJsonPath for checking that it is not in json body

    0 讨论(0)
提交回复
热议问题