SpringBootTest:没有“org.springframework.test.web.servlet.MockMvc”类型的合格 bean 可用:

问题描述 投票:0回答:6

嘿,我在创建测试用例时已经开始使用 spring boot 测试框架学习 spring-boot junit 测试,我面临以下问题。

    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")));
    }
}

在上面的代码中,我收到错误

    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]

我知道 spring-boot 找不到 MockMvc 名称 bean,但为什么它找不到它以及我该如何做才能使应用程序正常工作。

java spring-boot junit4 spring-boot-test mockmvc
6个回答
45
投票

希望你有

spring-boot-starter-web
依赖。不确定你使用的是哪个版本的 Spring boot,但是用这种方式构建mockMvc?

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

  @Autowired
  private WebApplicationContext webApplicationContext;
  private MockMvc mockMvc;

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

18
投票

尝试将以下注释添加到类中。

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

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

10
投票

我遇到了同样的问题,因为我正在遵循使用 WebFlux(反应式 Web)而不是同步 Web 的教程。假设单元测试可以同步,最后这对我有用(在 Gradle 中)

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

3
投票

我也遇到了同样的问题,因为我忘记从

application.properties
文件夹中的
test
文件中删除以下行:

spring.main.web-application-type=none

只需将其删除即可解决问题。


2
投票

我相信@karthik-r的答案就是!

我也遇到了同样的问题,所以有助于“调试”Spring 注入的内容的一件事就是。

@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);
}

当然如果你有的话

@Autowired
private WebApplicationContext webApplicationContext;

这将打印所有弹簧注入的豆子。 最后,如果您使用的是 JUnit 5,我还必须修改一件事。

将@RunWith更改为

@ExtendWith(SpringExtension.class)

就是这样...

=)


0
投票

这对我有用 https://github.com/spring-projects/spring-boot/issues/35873#issuecomment-1589885091

@ContextConfiguration
public class TestConfig {

@Bean
DynamicPropertyRegistry dynamicPropertyRegistry(ConfigurableEnvironment environment) {
    return TestcontainersPropertySource.attach(environment);
}
© www.soinside.com 2019 - 2024. All rights reserved.