title: Spring mocked junit tests throw NullPointerException with spring-security 3.2.0.RC1 version
body: <p>I have got a junit test which performs a request using Spring MockMvc.</p>
  
  <pre><code>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..)
  }
  </code></pre>
  
  <p>Test was working just fine, until I raised spring-security libs version to 3.2.0.RC1.
  Now, I am getting NullPointerException at:</p>
  
  <pre><code>ResultActions perform = mvc.perform(builder);
  </code></pre>
  
  <p>And the stacktrace is:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>I have no idea what to do about it. How to fix that test?
  I need this new version of spring-security because it gives support for CSRF attack prevetion.</p>
  
  <p>Greetings,
  Peter.</p>