SpringBootTest : No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available:

26,718

Solution 1

Hope you have spring-boot-starter-web dependency. Not sure which version of Spring boot you use, but build mockMvc this way instead?

@RunWith(SpringRunner.class)
@SpringBootTest
public class ApplicationTest {

  @Autowired
  private WebApplicationContext webApplicationContext;
  private MockMvc mockMvc;

  @Before
  public void setUp() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  }

Solution 2

Try adding the following annotations to the class.

import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc

Solution 3

I had the same issue, because I'm following a tutorial using WebFlux (reactive Web) instead of synchronous Web. Assuming that unit tests can be synchronous, finally this worked for me (in Gradle)

implementation 'org.springframework.boot:spring-boot-starter-webflux'
// Required for MockMvc autoconfigure
testImplementation 'org.springframework.boot:spring-boot-starter-web'
Share:
26,718

Related videos on Youtube

Prabhat Yadav
Author by

Prabhat Yadav

Updated on July 09, 2022

Comments

  • Prabhat Yadav
    Prabhat Yadav almost 2 years

    Hey i have started learing spring-boot junit testing using spring boot Test framework at the time of creating the test case i am facing issues below .

        import static org.hamcrest.Matchers.containsString;
        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.content;
        import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
    
        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.test.context.junit4.SpringRunner;
        import org.springframework.test.web.servlet.MockMvc;
    
    
        @RunWith(SpringRunner.class)
        @SpringBootTest
        @AutoConfigureMockMvc
        public class ApplicationTest {
    
        @Autowired
        private MockMvc mockMvc;
    
        @Test
        public void shouldReturnDefaultMessage() throws Exception {
            this.mockMvc.perform(get("/")).andDo(print()).andExpect(status().isOk())
                    .andExpect(content().string(containsString("Hello World")));
        }
    }
    

    In Above Code i am getting Error of

        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)}**
            at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1486) ~[spring-beans-4.3.8.RELEASE.jar:4.3.8.RELEASE]
    

    I am aware of MockMvc name bean is not found by spring-boot but why its not able to find it and how i can do so that application will work fine.

    • Karthik R
      Karthik R almost 6 years
      Are you using embedded tomcat or external tomcat instance with Servlet intitializer?
    • Prabhat Yadav
      Prabhat Yadav almost 6 years
      embedded tomcat : D:\mavenrepo\org\springframework\boot\spring-boot-starter-to‌​mcat\1.5.3.RELEASE\s‌​pring-boot-starter-t‌​omcat-1.5.3.RELEASE.‌​jar D:\mavenrepo\org\apache\tomcat\embed\tomcat-embed-core\8.5.1‌​4\tomcat-embed-core-‌​8.5.14.jar D:\mavenrepo\org\apache\tomcat\embed\tomcat-embed-el\8.5.14\‌​tomcat-embed-el-8.5.‌​14.jar D:\mavenrepo\org\apache\tomcat\embed\tomcat-embed-websocket\‌​8.5.14\tomcat-embed-‌​websocket-8.5.14.jar
  • Prabhat Yadav
    Prabhat Yadav almost 6 years
    using this getting org.springframework.beans.factory.UnsatisfiedDependencyExcep‌​tion: Error creating bean with name 'com.hanselnpetal.ApplicationTest': Unsatisfied dependency expressed through field 'webApplicationContext'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionExcept‌​ion: No qualifying bean of type 'org.springframework.web.context.WebApplicationContext' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(req‌​uired=true)}
  • Karthik R
    Karthik R almost 6 years
    Please remove the @AutoConfigureMockMvc and try with my solution.
  • Karthik R
    Karthik R almost 6 years
    Something with ur dependency? you have spring-boot-starter-web right? This just works fine for me.
  • trevorsky
    trevorsky over 5 years
    Dependency on spring-boot-starter-web solved this issue for me
  • Karthik R
    Karthik R over 5 years
    @trevorsky , please upvote if resolved. I will add this dependency check as update.
  • djangofan
    djangofan over 4 years
    Tried it. Now I get a different error: java.lang.Exception: No runnable methods
  • BreenDeen
    BreenDeen about 4 years
    I have all of that in my code. I simply posted the relevant portions. I have found a solution.
  • Jhonatan S. Souza
    Jhonatan S. Souza almost 3 years
    Worked to me! Im using webflux and now works.