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

标签 java spring-boot junit4 spring-boot-test mockmvc

嘿,在创建测试用例时,我已经开始使用 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,但为什么它无法找到它,以及我如何才能使应用程序正常工作。

最佳答案

希望你有 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();
  }

关于java - SpringBootTest : No qualifying bean of type 'org.springframework.test.web.servlet.MockMvc' available:,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51299015/

相关文章:

java - @Produces 方法中 @Mock 的 Junit 4 范围

java - 在所有jsp中添加一个jsp作为header

java - 在共享托管解决方案中的 JAVA 中托管 Restful Web 服务的位置

Spring Boot yml ResourceBundle 文件

java - 带有 pom.xml 和 app.java 的简单 spring-boot 应用程序显示构建失败错误

java - 单元测试多线程API

java - HttpUrlConnection 身份验证不起作用

java - 在同一端口下部署多个应用程序

java - 带选择的 CriteriaBuilder 不允许没有构造函数

java - JUnit 断言 : make the assertion between floats