Hot questions for Using Mockito in spring test mvc

Top 10 Java Open Source / Mockito / spring test mvc


I have an api call as:

@RequestMapping(value = "/course", method = RequestMethod.GET)
ResponseEntity<Object> getCourse(HttpServletRequest request, HttpServletResponse response) throwsException {
        User user = userDao.getByUsername(request.getRemoteUser());


I'm getting the user null when I call this from the test class like:

HttpServletRequest request = Mockito.mock(HttpServletRequest.class);

    MvcResult result =  mockMvc.perform( get( "/course")
                    .andExpect( status().isOk() )
                    .andExpect( content().contentType( "application/json;charset=UTF-8" ) )

When I debug request object I can see remoteUser=null. So how can I pass the value to remote user?


You can use RequestPostProcessor in order to modify the MockHttpServletRequest in any fashion you want. In your case:

mockMvc.perform(get("/course").with(request -> {
                    return request;

And if you're stuck with older versions of Java:

mockMvc.perform(get("/course").with(new RequestPostProcessor() {
            public MockHttpServletRequest postProcessRequest(MockHttpServletRequest request) {
                return request;


I have an application that uses Spring MVC for running a REST service (without Spring Boot). Context is mainly loaded from parents one. I have a controller and I would like to test it via MockMVC.

I have tried to set up local test context by hands, but it wasn't enough for running test. I assume, there should be extra beans I haven't set up.

My controller is:

public class ProrertyEditorController extends AbstractPropertyEditorController {

    protected PropertyEditorService prorertyEditorService;

    @RequestMapping(method = RequestMethod.DELETE, value = "/{dataType}/deletewithcontent")
@ResponseStatus(value = HttpStatus.OK)
public void deleteWithContent(@PathVariable("dataType") String dataType, @RequestParam("deleteall") boolean deleteAllContent, @RequestBody String node) {
    try {
        JSONArray itemsToDelete = new JSONArray(node);
        prorertyEditorService.deleteItemsWithContent(dataType, itemsToDelete, deleteAllContent);
    } catch (Exception e) {
        //handling exception

Up to this moment, test for controller looks like this:

public class ProrertyEditorControllerTest{
    private MockMvc mockMvc;

    private PropertyEditorService mockService;
    private ProrertyEditorController controller;

    public void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(new ProrertyEditorController()).build();

    public void deleteWithContentTest() throws Exception {
                   mockMvc.perform(delete("/full/path/{dataType}/deletewithcontent", type)
                .param("deleteall", "true")
                .param("node", "[{\"test key1\":\"test value1\"}, {\"test keys2\":\"test value2\"}]"));

        verify(mockService, times(1)).deleteItemsWithContent(eq("promotion"), eq(new JSONArray("[{\"test key1\":\"test value1\"}, {\"test keys2\": \"test value2\"}]")), eq(true));

Unfortunately, it doesn't work due to

Failed to load ApplicationContext

and no bean is created

PS There is an option to use

MockHttpServletRequest request = new MockHttpServletRequest();
MockHttpServletResponse response = new MockHttpServletResponse();

However, it requires refactoring of controllers method, that is not possible


It turned out that it is absolutely possible to do it. There are just a few configs required to launch it.

  1. You will need spring-test in your pom.xml to make it work

  2. Create a testContext.xml file. In my case, it was literally empty(!):

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns=""

Nevertheless, it is still required, otherwise, MockMVC will not start since there is no context.

  1. Config your controllerTest class with the following annotations:

    @ContextConfiguration(locations = "classpath*:testContextConfig.xml")
    public class ControllerTest {        ...    }

It should me mentioned that without @ContextConfiguration MockMVC won't work.

  1. Create a MockMVC instance in @Before method:

    private MockMvc mockMvc;
    private Service mockService;
    public void setup() {
        mockMvc = MockMvcBuilders.standaloneSetup(new Controller(mockService))
                .setHandlerExceptionResolvers(exceptionResolver()) //crutial for standaloneSetup of MockMVC

As far as I've understood, setHandlerExceptionResolvers is a crucial part of mockMVC setup.

Basically, that's it.