【问题标题】:springSecurityFilterChain cannot be nullspringSecurityFilterChain 不能为空
【发布时间】:2016-08-18 00:53:18
【问题描述】:

我正在使用 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

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

【问题讨论】:

    标签: spring spring-security spring-boot


    【解决方案1】:

    我想你想念@SpringBootTest

    改变这个

    @WebMvcTest(HelloController.class)
    

    有了这个

    @SpringBootTest
    

    【讨论】:

      猜你喜欢
      • 2020-11-28
      • 2016-05-10
      • 1970-01-01
      • 1970-01-01
      • 2015-01-01
      • 2016-03-04
      • 1970-01-01
      • 2018-01-23
      • 2017-06-16
      相关资源
      最近更新 更多