title: Spring security test with userDetailsService gives illegalStateException in test case
body: <p>I have some spring controller tests that worked fine before. 
  I recently added authentication using a userDetailsService, and now when i run controller tests it says this:</p>
  
  <pre><code>java.lang.IllegalStateException: Failed to load ApplicationContext
  ...
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'nl.kars.lms.service.MyUserDetailsService' available: 
  expected at least 1 bean which qualifies as autowire candidate.
  </code></pre>
  
  <p>I don't understand why, because everything should be configured correctly. It only happens when running the controller tests, running the app works perfectly fine. Here are my classes.</p>
  
  <p><strong>Test case</strong></p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @WebMvcTest(ActionController.class)
  public class ActionControllerTests {
  
      @Autowired
      private MockMvc mvc;
  
      @MockBean
      private ActionService service;
  
      @Test
      public void testGetActions_returns_result_from_service() throws Exception {
          int actionId = 1;
          Action action = new Action();
          action.setId(actionId);
          List&lt;Action&gt; actionsList = Arrays.asList(action);
  
          given(service.getActions()).willReturn(actionsList);
  
          mvc.perform(get("/actions")
                  .contentType(MediaType.APPLICATION_JSON))
                  .andExpect(status().isOk())
                  .andExpect(jsonPath("$", hasSize(1)))
                  .andExpect(jsonPath("$[0].id", Matchers.is(actionId)));
      }
  }
  </code></pre>
  
  <p><strong>Configuration</strong></p>
  
  <pre><code>@EnableWebSecurity
  @Configuration
  public class SecurityConfiguration extends WebSecurityConfigurerAdapter {
  
      @Autowired
      MyUserDetailsService userDetailsService;
  
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userDetailsService);
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
          http.cors();
          http.csrf().disable()
                  .authorizeRequests()
                  .antMatchers("/**")
                  .fullyAuthenticated()
                  .and().httpBasic();
      }
  
      @Bean
      public PasswordEncoder getPasswordEncoder() {
          return NoOpPasswordEncoder.getInstance();
      }
  }
  </code></pre>
  
  <p><strong>UserDetailsService</strong></p>
  
  <pre><code>@Service("userDetailsService")
  public class MyUserDetailsService implements UserDetailsService {
  
      @Autowired
      private EmployeeService employeeService;
  
      @Override
      public UserDetails loadUserByUsername(String email) throws UsernameNotFoundException {
          return new EmployeeDetails(employeeService.getEmployeeByEmail(email));
      }
  }
  </code></pre>
  
  <p>My question is, how do i stop the error from occuring? What am i doing wrong? I am lost at this point.
  Thanks</p>