Issue with responseEntity . MockMvc.Perform() gives 200 status code even if the ResponseEntity.HttpStatus is 201

Viewed 1304

Im trying to mock a controller's post method. I want it to return status code 201(CREATED) . But it gives Status Code 200. Given below is the code This is my controller Class

package com.example.demo;

import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

public @RestController
class SomeController{

    @PostMapping("/insertString")
    public ResponseEntity<String> insertString(String str) {
        return new ResponseEntity<>(new String("monster"),HttpStatus.CREATED);
    }
}

This is my junit test Suit

package com.example.demo;

import static org.junit.Assert.*;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.mockito.Mockito.anyString;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;   

@RunWith(SpringRunner.class)
@WebMvcTest(SomeController.class)
public class TestClass {

@Autowired
private MockMvc mockMvc;

@MockBean
private SomeController someController;

@Test
public void test() throws Exception {

    ResponseEntity<String> entity = new ResponseEntity<>(HttpStatus.CREATED);

    when(someController.insertString(anyString())).thenReturn(entity);

    RequestBuilder requestBuilder = MockMvcRequestBuilders
            .post("/insertString")
            .accept(MediaType.APPLICATION_JSON)
            .content("monkey")
            .contentType(MediaType.APPLICATION_JSON);

    MvcResult result = mockMvc.perform(requestBuilder)
            .andExpect(status().isCreated())
            .andReturn();
    System.out.println(result.getResponse().getContentAsString());
}

}

The errorim getting is

java.lang.AssertionErrror:Status expected:<201> but was:<200>

Though i have mentioned the status as HttpStatus.CREATED in the RequestEntity object, Why am i getting 200. Please if someone can help

1 Answers

Try to change

public ResponseEntity<String> insertString(String str)

to

public ResponseEntity<String> insertString(@RequestBody String str)

I've run the test with above code and it passed.

Related