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

Pra*_*dav 2 junit4 spring-boot mockmvc spring-boot-test

嘿,在创建测试用例时,我已经开始使用spring boot Test框架进行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")));
    }
}
Run Code Online (Sandbox Code Playgroud)

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

    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]
Run Code Online (Sandbox Code Playgroud)

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

Ana*_*u C 18

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

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

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@AutoConfigureMockMvc
Run Code Online (Sandbox Code Playgroud)


Kar*_*k R 6

希望你有spring-boot-starter-web依赖性。不知道您使用的是哪个版本的Spring Boot,而是用这种方式来构建ockMockMvc?

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

  @Autowired
  private WebApplicationContext webApplicationContext;
  private MockMvc mockMvc;

  @Before
  public void setUp() {
    mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
  }
Run Code Online (Sandbox Code Playgroud)

  • 使用此获取 org.springframework.beans.factory.UnsatisfiedDependencyException: 创建名称为“com.hanselnpetal.ApplicationTest”的 bean 时出错:通过字段“webApplicationContext”表示不满足的依赖关系;嵌套异常是 org.springframework.beans.factory.NoSuchBeanDefinitionException:没有可用的“org.springframework.web.context.WebApplicationContext”类型的合格 bean:预计至少有 1 个有资格作为自动装配候选者的 bean。依赖注释:{@org.springframework.beans.factory.annotation.Autowired(required=true)} (2认同)
  • 对spring-boot-starter-web的依赖为我解决了这个问题 (2认同)

Sou*_*rer 5

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

implementation 'org.springframework.boot:spring-boot-starter-webflux'
// Required for MockMvc autoconfigure
testImplementation 'org.springframework.boot:spring-boot-starter-web'
Run Code Online (Sandbox Code Playgroud)