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

6

42

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.

Hardihood answered 12/7, 2018 at 6:52 Comment(2)
Are you using embedded tomcat or external tomcat instance with Servlet intitializer?Protium
embedded tomcat : D:\mavenrepo\org\springframework\boot\spring-boot-starter-tomcat\1.5.3.RELEASE\spring-boot-starter-tomcat-1.5.3.RELEASE.jar D:\mavenrepo\org\apache\tomcat\embed\tomcat-embed-core\8.5.14\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.jarHardihood
P
45

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();
  }
Protium answered 12/7, 2018 at 7:28 Comment(9)
using this getting org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'com.hanselnpetal.ApplicationTest': Unsatisfied dependency expressed through field 'webApplicationContext'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: 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(required=true)}Hardihood
Please remove the @AutoConfigureMockMvc and try with my solution.Protium
Something with ur dependency? you have spring-boot-starter-web right? This just works fine for me.Protium
Dependency on spring-boot-starter-web solved this issue for meElide
@Elide , please upvote if resolved. I will add this dependency check as update.Protium
Tried it. Now I get a different error: java.lang.Exception: No runnable methodsCatherine
I have all of that in my code. I simply posted the relevant portions. I have found a solution.Erse
works fine with version 3.x .. it solved issue with maven dependencies that are using spring ...Tham
Still works 3.1.3Race
A
18

Try adding the following annotations to the class.

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
Akins answered 30/8, 2021 at 6:6 Comment(0)
H
10

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'
Haworth answered 30/11, 2020 at 9:27 Comment(1)
Worked to me! Im using webflux and now works.Evadnee
W
3

I also had the same issue because I forgot to remove the following line from the application.properties file in the test folder:

spring.main.web-application-type=none

Simply removing it solved the problem.

Wile answered 19/10, 2022 at 12:34 Comment(0)
B
2

I believe that the answer of @karthik-r is the one!

I also had the same problem, so one thing that helps to "debug" what was beeing injected by Spring was that.

@BeforeEach
void printApplicationContext() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    //This  
    Arrays.stream(webApplicationContext.getBeanDefinitionNames())
            .map(name -> webApplicationContext.getBean(name).getClass().getName())
            .sorted()
            .forEach(System.out::println);
}

Of course if you have

@Autowired
private WebApplicationContext webApplicationContext;

This will print all spring injected beans. And finaly one more thing that i had to modify and if you are using JUnit 5.

Change the @RunWith to

@ExtendWith(SpringExtension.class)

That is it...

=)

Brawner answered 17/9, 2021 at 21:25 Comment(0)
E
0

For the error, NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.test.context.DynamicPropertyRegistry'

This is what worked for me https://github.com/spring-projects/spring-boot/issues/35873#issuecomment-1589885091

@ContextConfiguration
public class TestConfig {

@Bean
DynamicPropertyRegistry dynamicPropertyRegistry(ConfigurableEnvironment environment) {
    return TestcontainersPropertySource.attach(environment);
}
Exoenzyme answered 13/5, 2024 at 12:23 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.