title: Spring Boot Testing - No Qualifying Bean Exception
body: <p>While attempting some tests using the Spring Boot framework, I am running into an issue with finding a Bean that the test unit depends on.</p>
  
  <pre><code>Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'authServerApplication': Unsatisfied dependency expressed through field 'passwordEncoder'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'org.springframework.security.crypto.password.PasswordEncoder' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
  </code></pre>
  
  <p>My test class:</p>
  
  <pre><code>    @RunWith(SpringRunner.class)
  @DataJpaTest
  @SpringBootTest
  
  public class UserDetailsTest {
  
      @Autowired
      private TestEntityManager entityManager;
  
  //    @MockBean
  //    private PasswordEncoder passwordEncoder;
  
      @Autowired
      private UserRepository userRepo;
  
      @Test
      public void test() {
          OAuthUser user = null;
          this.entityManager.persist(new OAuthUser("Kelly", "Marchewa", "kmarchewa", "password"));
          user = userRepo.findByUserName("kmarchewa");
          System.out.println(user.getPassword());
          assertThat(user.getUserName()).isEqualTo("kmarchewa");
  
  
      }
  
  }
  </code></pre>
  
  <p>If I uncomment the @MockBean portion, the code will compile fine. However, I want to test the repository on its ability to encode and decode passwords too. To my understanding of <a href="https://docs.spring.io/spring-boot/docs/current/reference/html/boot-features-testing.html" rel="nofollow noreferrer">the documentation</a>, the @SpringBootTest annotation should be able to automatically "pick-up" the @Configuration classes. I have a main @SpringBootApplication:</p>
  
  <pre><code>@SpringBootApplication
  public class AuthServerApplication {
  
  @Autowired
  private PasswordEncoder passwordEncoder;
  
  public static void main(String[] args) {
      SpringApplication.run(AuthServerApplication.class, args);
  }
  
  @Bean
  public CommandLineRunner demo(UserRepository repository) {
      return(args) -&gt; {
          OAuthUser user = new OAuthUser();
  
  
          user.setFirstName("Kelly");
          user.setLastName("Marchewa");
          user.setPassword(passwordEncoder.encode("Admin"));
          user.setUserName("Admin");
  
      // repository.save(user);
      };
  }
  }
  </code></pre>
  
  <p>This Spring Boot Application depends on three other @Configuration classes: AppConfig, SecurityConfig, and AuthServerConfig. For this issue, the SecurityConfig and AppConfig classes are relevant (they include references to  the PasswordEncoder bean).</p>
  
  <p>AppConfig (partial)</p>
  
  <pre><code>@Configuration
  public class AppConfig {
  
          @Value("${spring.datasource.url}")
          private String datasourceUrl;
  
          @Value("${spring.datasource.driverClassName}")
          private String dbDriverClassName;
  
          @Value("${spring.datasource.username}")
          private String dbUsername;
  
          @Value("${spring.datasource.password}")
          private String dbPassword;
  
          @Bean
          public PasswordEncoder passwordEncoder() {
              return new BCryptPasswordEncoder();
          }
          /// more code here 
  }
  </code></pre>
  
  <p>SecurityConfig:</p>
  
  <pre><code>    @Configuration
  @EnableGlobalMethodSecurity(prePostEnabled = true)
  @EnableWebSecurity
  public class SecurityConfig extends WebSecurityConfigurerAdapter {
  
      @Autowired
      private OAuthUserDetailsService userService;
  
      @Autowired
      private PasswordEncoder passwordEncoder;
  
      @Bean
      @Override
      protected AuthenticationManager authenticationManager() throws Exception {
          return super.authenticationManager();
  }
  
      // Hash password
      @Override
      protected void configure(AuthenticationManagerBuilder auth) throws Exception {
          auth.userDetailsService(userService)
          .passwordEncoder(passwordEncoder);
      }
  
      @Override
      protected void configure(HttpSecurity http) throws Exception {
         http
                 .sessionManagement()
                 .sessionCreationPolicy(SessionCreationPolicy.STATELESS)
                 .and()
                 .httpBasic()
                 .realmName("test")
                 .and()
                 .csrf()
                 .disable();
  
      }
  }
  </code></pre>
  
  <p>The UserRepository class is very simple:</p>
  
  <pre><code>    public interface UserRepository extends CrudRepository&lt;OAuthUser, Long&gt; {
  
      public OAuthUser findByUserName(String name);
  }
  </code></pre>
  
  <p>How can I ensure all required beans are found for my tests?
  Thanks.</p>
  
  <p>EDIT:</p>
  
  <p>I receive the same error if I attempt to @Autowire the bean in my test class.</p>
  
  <pre><code>@Autowired 
  private PasswordEncoder passwordEncoder;
  </code></pre>