Spring Boot Test unable to autowired MockMvc

Spring Boot integeration test, but unable to @Autowired MockMvc

P.S Tested with Spring Boot 2.1.2.RELEASE

BookControllerTest.java

package com.mkyong;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class BookControllerTest {

    @Autowired
    private MockMvc mockMvc; //null?

    @Test
    public void findOne() throws Exception {
        mockMvc.perform(get("/books/1"))
                .andDo(print())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id", is(1)));
    }

}

Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: 
	No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available: 
	expected at least 1 bean which qualifies as autowire candidate. 
	Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
	
	//...
	... 28 more

Solution

Add this @AutoConfigureMockMvc to enable and configure auto-configuration of MockMvc


package com.mkyong;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.MediaType;
import org.springframework.security.test.context.support.WithMockUser;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;

import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;

@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc //need this in Spring Boot test
public class BookControllerTest {

    @Autowired
    private MockMvc mockMvc;

    @Test
    public void findOne() throws Exception {
        mockMvc.perform(get("/books/1"))
                .andDo(print())
                .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
                .andExpect(status().isOk())
                .andExpect(jsonPath("$.id", is(1)));
    }

}

References

4 comments on “Spring Boot Test unable to autowired MockMvc

  1. Forex: My API will return List<Map<Object, Object>> the how can I get the List from mockMvc response so that I can assert on required part.

    How to get the response object?
    Is below is the correct way?

    MvcResult andReturn = mockMvc.perform(get(url)).andDo(print()).andExpect(status().isOk()).andReturn();
    MockHttpServletResponse response = andReturn.getResponse();
    String contentAsString = response.getContentAsString();

    And also Can we get the code coverage with MockMvc?

    Reply

Leave a Comment

Your email address will not be published. Required fields are marked *