Spring模拟的junit测试使用spring-security 3.2.0.RC1版本抛出NullPointerException

时间:2013-11-27 11:53:49

标签: java junit spring-security mocking spring-test

我有一个junit测试,它使用Spring MockMvc执行请求。

private MockMvc mvc;

@Before
public void setUp() {
    mvc = MockMvcBuilders.standaloneSetup(new LoginController())
            .addFilter(new AnonymousAuthenticationFilter("anonymous"))
            .addFilter(new SecurityContextHolderAwareRequestFilter())
            .addFilter(new XssValidationFilter()).build();
    SecurityContextHolder.getContext().setAuthentication(null);
}

@Test
public void test() throws Exception {
    MockHttpServletRequestBuilder builder = MockMvcRequestBuilders.get("/login.htm")
            .param(USER, "admin").param(PASSWORD, "pass");
    ResultActions perform = mvc.perform(builder);
    (assertions..)
}

测试工作正常,直到我将spring-security libs版本提升到3.2.0.RC1。 现在,我得到NullPointerException:

ResultActions perform = mvc.perform(builder);

堆栈跟踪是:

java.lang.NullPointerException
at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:150)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137)
at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:113)
at org.springframework.mock.web.MockFilterChain.doFilter(MockFilterChain.java:137)
at org.springframework.test.web.servlet.MockMvc.perform(MockMvc.java:141)
at com.mycompany.controller.main.LoginControllerTest.test(LoginControllerTest.java:134)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:601)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
at org.springframework.test.context.junit4.statements.RunBeforeTestMethodCallbacks.evaluate(RunBeforeTestMethodCallbacks.java:74)
at org.springframework.test.context.junit4.statements.RunAfterTestMethodCallbacks.evaluate(RunAfterTestMethodCallbacks.java:83)
at org.springframework.test.context.junit4.statements.SpringRepeat.evaluate(SpringRepeat.java:72)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:230)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.runChild(SpringJUnit4ClassRunner.java:87)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
at org.springframework.test.context.junit4.statements.RunBeforeTestClassCallbacks.evaluate(RunBeforeTestClassCallbacks.java:61)
at org.springframework.test.context.junit4.statements.RunAfterTestClassCallbacks.evaluate(RunAfterTestClassCallbacks.java:71)
at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
at org.springframework.test.context.junit4.SpringJUnit4ClassRunner.run(SpringJUnit4ClassRunner.java:173)
at org.eclipse.jdt.internal.junit4.runner.JUnit4TestReference.run(JUnit4TestReference.java:50)
at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:467)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:683)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:390)
at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:197)

我不知道该怎么做。如何修复该测试? 我需要这个新版本的spring-security,因为它支持CSRF攻击预防。

问候, 彼得。

1 个答案:

答案 0 :(得分:0)

正如M. Deinum所说,我不得不在SecurityContextHolderAwareRequestFilter上调用afterPropertiesSet方法。所以setUp方法看起来像这样:

@Before
public void setUp() throws ServletException {
    Mockito.reset(userGroupInfo, licenseManager);
    SecurityContextHolderAwareRequestFilter securityContextHolderAwareRequestFilter = new SecurityContextHolderAwareRequestFilter();
    securityContextHolderAwareRequestFilter.afterPropertiesSet();
    mvc = MockMvcBuilders.standaloneSetup(new LoginController(userGroupInfo, licenseManager, userInfo))
            .addFilter(new AnonymousAuthenticationFilter("anonymous"))
            .addFilter(securityContextHolderAwareRequestFilter)
            .addFilter(new XssValidationFilter()).build();
    SecurityContextHolder.getContext().setAuthentication(null);
}