Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

springSecurityFilterChain cannot be null

I'm working with Spring Boot 1.4.0.M2 and I have written the following test case to ensure the proper functioning of a controller with spring security.

@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"));
    }
}

When I run the test, the following exception is thrown:

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.

All other code can be found here: https://github.com/renewinkler84/http-cache-demo

Why is this exception thrown? what else I have to configure?

like image 540
René Winkler Avatar asked Apr 24 '16 18:04

René Winkler


1 Answers

I think you miss @SpringBootTest

change this

@WebMvcTest(HelloController.class)

with this

@SpringBootTest
like image 124
kakashi hatake Avatar answered Nov 16 '22 03:11

kakashi hatake