76

This is my method inside my controller which is annotated by @Controller

@RequestMapping(value = "/getServerAlertFilters/{serverName}/", produces = "application/json; charset=utf-8")
    @ResponseBody
    public JSONObject getServerAlertFilters(@PathVariable String serverName) {
        JSONObject json = new JSONObject();
        List<FilterVO> filteredAlerts = alertFilterService.getAlertFilters(serverName, "");
        JSONArray jsonArray = new JSONArray();
        jsonArray.addAll(filteredAlerts);
        json.put(SelfServiceConstants.DATA, jsonArray);
        return json;
    }

I am expecting {"data":[{"useRegEx":"false","hosts":"v2v2v2"}]} as my json.

And this is my JUnit test:

@Test
    public final void testAlertFilterView() {       
        try {           
            MvcResult result = this.mockMvc.perform(get("/getServerAlertFilters/v2v2v2/").session(session)
                    .accept("application/json"))
                    .andDo(print()).andReturn();
            String content = result.getResponse().getContentAsString();
            LOG.info(content);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

Here is the console output:

MockHttpServletResponse:
              Status = 406
       Error message = null
             Headers = {}
        Content type = null
                Body = 
       Forwarded URL = null
      Redirected URL = null
             Cookies = []

Even result.getResponse().getContentAsString() is an empty string.

Can someone please suggest how to get my JSON in my JUnit test method so that I can complete my test case.

1
  • Please note that you received an http error code 406 (not acceptable request error), that's why your body is empty
    – bdzzaid
    Mar 8, 2019 at 6:16

5 Answers 5

89

I use TestNG for my unit testing. But in Spring Test Framework they both looks similar. So I believe your test be like below

@Test
public void testAlertFilterView() throws Exception {
    this.mockMvc.perform(get("/getServerAlertFilters/v2v2v2/").
            .andExpect(status().isOk())
            .andExpect(content().json("{'data':[{'useRegEx':'false','hosts':'v2v2v2'}]}"));
    }

If you want check check json Key and value you can use jsonpath .andExpect(jsonPath("$.yourKeyValue", is("WhatYouExpect")));

You might find thatcontent().json() are not solveble please add

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

3
  • 2
    I'm getting NoClassDefFoundError: org/skyscreamer/jsonassert/JSONAssert when tried the solution (used in spring's JsonExpectationsHelper) Jan 31, 2019 at 14:53
  • It can happen due to different reasons. stackoverflow.com/a/5756989/2940265 Feb 1, 2019 at 2:57
  • @user7294900 possibly you should add jsonassert library to your runtime dependencies. For example from here. Jun 20, 2019 at 17:02
11

The 406 Not Acceptable status code means that Spring couldn't convert the object to json. You can either make your controller method return a String and do return json.toString(); or configure your own HandlerMethodReturnValueHandler. Check this similar question Returning JsonObject using @ResponseBody in SpringMVC

9

You can try the below for get and post methods

@Autowired
private MuffinRepository muffinRepository;

@Test
public void testGetMethod throws Exception(){
    Muffin muffin = new Muffin("Butterscotch");
    muffin.setId(1L);
    
    BddMockito.given(muffinRepository.findOne(1L)).
        willReturn(muffin);
        
    mockMvc.perform(MockMvcRequestBuilders.
        get("/muffins/1")).
        andExpect(MockMvcResutMatchers.status().isOk()).
        andExpect(MockMvcResutMatchers.content().string("{\"id\":1, "flavor":"Butterscotch"}"));    
}

//Test to do post operation
@Test
public void testPostMethod throws Exception(){
    Muffin muffin = new Muffin("Butterscotch");
    muffin.setId(1L);
    
    BddMockito.given(muffinRepository.findOne(1L)).
        willReturn(muffin);
        
    mockMvc.perform(MockMvcRequestBuilders.
        post("/muffins")
        .content(convertObjectToJsonString(muffin))
        .contentType(MediaType.APPLICATION_JSON)
        .accept(MediaType.APPLICATION_JSON))
        .andExpect(MockMvcResutMatchers.status().isCreated())
        .andExpect(MockMvcResutMatchers.content().json(convertObjectToJsonString(muffin))); 
}

If the response is empty then make sure to override equals() and hashCode() methods on the Entity your repository is working with:

//Converts Object to Json String
private String convertObjectToJsonString(Muffin muffin) throws JsonProcessingException{
    ObjectWriter writer = new ObjectWriter().writer().withDefaultPrettyPrinter();
    return writer.writeValueAsString(muffin);
}
4

If you want to check a few values in a specific field of JSON

.andExpect(MockMvcResultMatchers.jsonPath("$.message",
    AllOf.allOf(
        StringContains.containsString("name: must not be null"),
        StringContains.containsString("type: must not be null")
    )));

How it looks in the test class. JUnit4.

import com.fasterxml.jackson.databind.ObjectMapper;
import org.hamcrest.core.AllOf;
import org.hamcrest.core.StringContains;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;

@RunWith(MockitoJUnitRunner.class)
public class YourControllerTest {

  @Mock
  private YourService service;

  private MockMvc mvc;

  @Before
  public void setUp() {
    MockitoAnnotations.initMocks(this);
    mvc = MockMvcBuilders
        .standaloneSetup(new YourController(service))
        .setControllerAdvice(new YourExceptionHandler())
        .setCustomArgumentResolvers(new PageableHandlerMethodArgumentResolver())
        .build();
  }

  @Test
  public void yourControllerMethodName_400_validation() throws Exception {
    String path = "/orders/{orderId}/items";
    Integer orderId = 123;

    YourRequestDto requestDto = YourTestFactory.buildYourRequestDto();
    requestDto.setName(null);
    requestDto.setType(null);

    YourResponseDto expected = YourTestFactory.buildYourResponseDto(requestDto);

    Mockito
        .when(service.someMethod(orderId, requestDto))
        .thenReturn(expected);

    mvc
        .perform(
            MockMvcRequestBuilders.post(path, orderId)
                .contentType(MediaType.APPLICATION_JSON)
                .content(new ObjectMapper().writeValueAsString(requestDto))
        )
        .andExpect(MockMvcResultMatchers.status().isBadRequest())
        .andExpect(MockMvcResultMatchers.jsonPath("$.message",
            AllOf.allOf(
                StringContains.containsString("name: must not be null"),
                StringContains.containsString("type: must not be null")
            )));
  }
}
1
4

There are 2 ways to check JSON responses. Lemme guide you through both of them, (taking test method from the question above, and assuming response {"data":[{"useRegEx":"false","hosts":"v2v2v2"}]} as given above)

Method 1) Asserting complete JSON

@Test
public final void testAlertFilterView() {       
    mockMvc.perform(get("/getServerAlertFilters/v2v2v2/")
           .contentType("application/json"))
           .andExpect(status().isOk())
           // you may even read bigger json responses from file and convert it to string, instead of simply hardcoding it in test class
           .andExpect(content().json("{"data":[{"useRegEx":"false","hosts":"v2v2v2"}]}"))     
}

Method 2) Asserting specific key-value of response (not writing redundant piece of code)

.andExpect(jsonPath("$.data[0].useRegEx").value(false))
.andExpect(jsonPath("$.data[0].hosts").value("v2v2v2"));

Another thing you might need is the import statement,

import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.