title: @WithUserDetails unable to create SecurityContext
body: <p>I am trying to test whether certain url are only accessible to authorized users with certain privileges. Because I have my own <code>UserDetails</code>, I am annotating my tests with <code>@WithUserDetails</code> following the instructions in <a href="http://docs.spring.io/spring-security/site/docs/current/reference/html/test-mockmvc.html" rel="nofollow noreferrer">the docs</a></p>
  
  <p>My test class looks like:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes = { Application.class, ConfigTest.class }, webEnvironment = 
      WebEnvironment.RANDOM_PORT)
  @Transactional
  public class SecurityIntegrationTest {
  
      @Autowired
      private WebApplicationContext webApplicationContext;
  
      private MockMvc mockMvc;
  
      @Before
      public void setUp() {
           mockMvc=MockMvcBuilders.webAppContextSetup(webApplicationContext).apply(springSecurity()).build();
      }
  
      @Test
      @WithUserDetails(userDetailsServiceBeanName="myUserDetailsService")
      public void testTemp() throws Exception {
          this.mockMvc.perform( get("/")).andDo(print()).andExpect(status().isOk());          
      }
  }
  </code></pre>
  
  <p>The UserDetails Bean from the Config looks like:</p>
  
  <pre><code>@Configuration
  public class ConfigTest extends WebMvcConfigurerAdapter {
      public ConfigTest() {
         super();
      }    
  
      @Bean
      public UserDetailsService myUserDetailsService() {
          return new MyUserDetailsService();
      }
  
  }
  </code></pre>
  
  <p>If I try to run the test I get an exception:</p>
  
  <pre><code>TestContextManager - Caught exception while allowing TestExecutionListener [org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener@1ad535f] to process 'before' execution of test method [public void org.trip.test.SecurityIntegrationTest.testTemp() throws java.lang.Exception] for test instance [org.trip.test.SecurityIntegrationTest@167181f]
  j.l.IllegalStateException: Unable to create SecurityContext using @org.springframework.security.test.context.support.WithUserDetails(userDetailsServiceBeanName=myUserDetailsService, value=user)
  </code></pre>
  
  <p>I suppose this means that there is a problem combining <code>@Before</code> and <code>@WithUserDetails</code> here. I also tried replacing <code>@Before</code> with <code>@BeforeTransaction</code>, but this did not help.</p>
  
  <p>I am not sure how to fix this, so would appreciate any help/suggestions/ideas. </p>
  
  <p>PS: Running the same text anonymously (without <code>@WithUserDetails</code>) works fine.</p>