问答详情
源自:2-7 单元测试

AutoConfigureMockMvc+SpringBootTest自动注入MockMvc始终报找不到MockMvc bean,求指导

https://img1.mukewang.com/5adf3e4a00019a0a18360570.jpg

问题如图,自动注入MockMvc失败

提问者:qq_流浪_1 2018-04-24 22:30

个回答

  • luo_24
    2018-04-25 21:12:22

    这是我可以运行的文件,希望对你有帮助...

    package com.chengsluo.demo.controller;
    
    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;
    import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
    import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
    
    @RunWith(SpringRunner.class)
    @SpringBootTest
    @AutoConfigureMockMvc
    
    public class GrilControllerTest {
    
       @Autowired
       private MockMvc mockMvc;
    
       @Test
       public void girlsList() throws Exception {
          mockMvc.perform(MockMvcRequestBuilders.get("/girls"))
                .andExpect(MockMvcResultMatchers.status().isOk());
    //          .andExpect(MockMvcResultMatchers.status().string("213"));
       }
    }