René Winkler
René Winkler

Reputation: 7058

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?

Upvotes: 8

Views: 11200

Answers (1)

kakashi hatake
kakashi hatake

Reputation: 1185

I think you miss @SpringBootTest

change this

@WebMvcTest(HelloController.class)

with this

@SpringBootTest

Upvotes: 12

Related Questions