springSecurityFilterChain不能为null

Ren*_*ler 5 spring spring-security spring-boot

我正在使用Spring Boot 1.4.0.M2,我编写了以下测试用例,以确保具有弹簧安全性的控制器正常运行.

@RunWith(SpringRunner.class)
@WebMvcTest(HelloController.class)
public class HelloControllerTest {

    @Autowired
    private WebApplicationContext wac;

    private MockMvc mockMvc;

    @Before
    public void setUp() {
        mockMvc = MockMvcBuilders
                .webAppContextSetup(wac)
                .alwaysDo(print())
                .apply(springSecurity())
                .build();
    }

    @Test
    public void getHelloShouldReturnWithCacheControlSet() throws Exception {
        this.mockMvc.perform(get("/hello")
                .accept(MediaType.TEXT_PLAIN))
                .andExpect(status().isOk())
                .andExpect(content().string("Hello World!"))
                .andExpect(header().stringValues("Cache-Control", "max-age=5"));
    }
}
Run Code Online (Sandbox Code Playgroud)

当我运行测试时,抛出以下异常:

java.lang.IllegalStateException: springSecurityFilterChain cannot be null. Ensure a Bean with the name springSecurityFilterChain implementing Filter is present or inject the Filter to be used.
Run Code Online (Sandbox Code Playgroud)

所有其他代码可以在这里找到:https://github.com/renewinkler84/http-cache-demo

为什么抛出这个异常?还有什么我需要配置?

kak*_*ake 8

我想你想念 @SpringBootTest

改变这一点

@WebMvcTest(HelloController.class)
Run Code Online (Sandbox Code Playgroud)

有了这个

@SpringBootTest
Run Code Online (Sandbox Code Playgroud)