dimanche 27 décembre 2015

How to extract JSON value explicitly, which is checked by MockMvcResultMatchers#jsonPath?

I was writing a test by sample from here. The test was intended to check, that root's username is equal to it's username from database and looked following:

import static org.junit.Assert.*;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

...

@Test
   public void rootUserPresent() throws Exception {

      ResultActions result = mockMvc.perform(get("/user/root"));

      result
         .andExpect(status().isOk())
         .andExpect(content().contentType(contentType))
         .andExpect(jsonPath("$.screenName", is(userRepository.getRootUser().getScreenName())))
         ;

   }

First I wrote this test, it was causing ClassNotFound exception

java.lang.NoClassDefFoundError: com/jayway/jsonpath/InvalidPathException

So, I was thinking the system wishes to report me about wrong path but can't find class for an exception. So, I included com.jayway.jsonpath:json-path-assert:1.1.0 dependency. After that the test just began to be PASSED.

So, I came to suspicion, that test result is wrong positive.

My question is: how explicitly extract JSON value with the same instruments I have here, and check it's value literally?

PS

JSON result is following:

{
   id: 1,
   roles: [
   {
      name: "USER"
   },
   {
      name: "ADMIN"
   }
],
   firstName: null,
   lastName: null,
   screenName: "root",
}

Aucun commentaire:

Enregistrer un commentaire