SpringSecurityFilterChain cannot be null

I work with Spring Boot 1.4.0.M2, and I wrote the following test case to ensure that the controller functions correctly 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 codes can be found here: https://github.com/renewinkler84/http-cache-demo

Why is this exception thrown? What else do I need to configure?

+5
source share
1 answer

I guess you miss @SpringBootTest

change this value

 @WebMvcTest(HelloController.class) 

with this

 @SpringBootTest 
+6
source

Source: https://habr.com/ru/post/1247823/


All Articles