8

This question has been asked before and I have tried their solution but that doesn't work for me, I am using MockMvc to unit test content type of my rest call. I am getting this exception:

java.lang.AssertionError: Content type not set

While I'm setting it in my search method using produces attribute.

This is the method where I am initializing the mocks:

@Before
public void init() {
    MockitoAnnotations.initMocks(this);
    ReflectionTestUtils.setField(restController, "luceneSearchEnabled", true);
    mockMvc = standaloneSetup(restController).build();
}

This is my test method:

@Test
public void pmmSearchContentTypeTest() throws Exception { 
    mockMvc
          .perform(get("/api/v1/pmm").contentType(MediaType.APPLICATION_JSON))
          .andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON_VALUE)
          .andReturn();
}

This is my search method where I am setting content type:

@RequestMapping(value = "/api/" + REST_API_VERSION + "/" + ONE_INTERFACE, method = RequestMethod.GET, produces ={MediaType.APPLICATION_JSON_VALUE})
@ResponseBody
public String pmmSearch() { ... }

I don't know what is wrong here.

Ali Dehghani
  • 46,221
  • 15
  • 164
  • 151
jack
  • 313
  • 2
  • 10

4 Answers4

4

I faced the same error and found that , the mock service for this controller method returns null. change mock service method to return values for any() input and test this to get rid of this error.

when(service.method(any())).thenReturn(someElement);

someElement was null earlier causing this error case

  • 2
    In my test, I found that the passed input didn't have any value. And so, it was giving this error:`java.lang.AssertionError: Content type not set`. – Stuti Verma Aug 02 '17 at 07:03
1

Figured it out myself

Instead of using the mock object of retcontroller here

mockMvc = standaloneSetup(restController).build();

I had to use a real object

mockMvc = standaloneSetup(new RestController()).build();

and in order to avoid spring validation error I had to use complete path here

mockMvc
.perform(get("/api/v1/pmm/search{}").contentType(MediaType.APPLICATION_JSON))
jack
  • 313
  • 2
  • 10
0

If the response returns null, you can get that error, as it says @stuti-verma. It happened to me right now.

The case: In my test, I was sending a json to the controller, but the object that I used to generate the mocked response didn't have the equals/hashCode implemented. So, it will never match with the received json.

@RequestBody anObject

mock.theMethod(anObject).thenReturn(theResponse)

anObject must have equals/hashCode to be able to be compared

vvalenciacruz
  • 341
  • 2
  • 8
0

In my case, I had to set the content type returned by the request in the ResponseEntity itself:

PaymentResponse paymentResponse = makePaymentService.makePayment(paymentRequest);
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/json");
return new ResponseEntity<>(paymentResponse, HttpStatus.OK);

see java.lang.AssertionError: Content Type Not Set - Spring Controller JUnit Tests