I need to test my REST controllers that they are protected with Spring Security. I use MockMvc as Spring Security Link offered here
http://docs.spring.io/spring-security/site/docs/4.0.x/reference/htmlsingle/#test-mockmvc
Test:
@ContextConfiguration(locations = "classpath:applicationContext.xml")
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
public class LikesTest {
protected MockMvc mockMvc;
@Autowired
private WebApplicationContext context;
@Before
public void setup() {
mockMvc = MockMvcBuilders
.webAppContextSetup(context)
.apply(SecurityMockMvcConfigurers.springSecurity())
.build();
}
@Test
@WithMockUser("user")
public void testAddLike() throws Exception {
mockMvc.perform(get("/like?msgId=4&like=false"));
}
}
When I run the JUnit test, I get this crash trace
java.lang.NullPointerException at org.springframework.security.web.FilterChainProxy.getFilters (FilterChainProxy.java:223)
Also, if you remove the bean inside applicationContext.xml:
<bean id="springSecurityFilterChain" class="org.springframework.security.web.FilterChainProxy"/>
Then I get this error:
java.lang.IllegalStateException: springSecurityFilterChain . , bean springSecurityFilterChain , . org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurer.beforeMockMvcCreated(SecurityMockMvcConfigurer.java:62)
, FilterChainProxy NULL. Web.xml DelegatingFilterProxy springSecurityFilterChain, . , !