springSecurityFilterChain不能为空

时间:2016-04-24 18:01:31

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

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

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.

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

为什么抛出此异常?还有什么我需要配置的?

1 个答案:

答案 0 :(得分:7)

我想你错过了@SpringBootTest

更改此

@WebMvcTest(HelloController.class)

用这个

@SpringBootTest