title: Spring Boot/JUnit - No qualifying bean of type 'boolean' available
body: <p>I have been using Spring for a number of months now, but still do not understand some of the various subtleties imposed by the framework.</p>
  
  <p>I am trying to execute a unit test for a class and am running into the often seen error message and exception:</p>
  
  <pre><code>***************************
  APPLICATION FAILED TO START
  ***************************
  
  Description:
  
  Parameter 1 of constructor in &lt;package&gt;.MessagingManager required a bean of type 'boolean' that could not be found.
  
  Action:
  
  Consider defining a bean of type 'boolean' in your configuration.
  
  Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'boolean' available: expected at least 1 bean which qualifies as autowire candidate.
  </code></pre>
  
  <p>The unit test class (<code>messagingManager</code> is the SUT):</p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @SpringBootTest(classes = {MessagingManager.class})
  public class TestMessagingManager {
  
    @Autowired
    private MessagingManager messagingManager;
    ...
  </code></pre>
  
  <p>A configuration class:</p>
  
  <pre><code>@Configuration
  @EnableScheduling
  @EnableAsync
  @ComponentScan({"&lt;package 1&gt;"})
  public class ServiceConfigurer {
    ...
    @Value("${service.name}")
    private String         serviceName;
    ...
    @Bean(name = "messagingManager")
    public MessagingManager messagingManager() {
      return new MessagingManager(serviceConfig(), "MISSION_PLANNER".equals(serviceName));
    }
    ...
    @Bean
    public ServiceConfig serviceConfig() {
      return config.getServiceByName(serviceName);
    }
    ...
  }
  </code></pre>
  
  <p>The SUT class:</p>
  
  <pre><code>public class MessagingManager {
    ...
    public MessagingManager(ServiceConfig serviceCfg, boolean transactional) {
      ...
    }
    ...
  }
  </code></pre>
  
  <p>I tried using <code>org.springframework.test.util.ReflectionTestUtils.setField()</code> to set the <code>serviceName</code> field in the <code>ServiceConfigurer</code> class, but that did not help. I suspect it doesn't work because the problem is occurring when DI is involved and that the reflection utility usage is "too late". Just a guess.</p>
  
  <p>What am I doing wrong?</p>
  
  <p>If I were to entertain the idea of "creating a bean of type boolean", how do I define a bean that simply holds a boolean value, that's set based on the value of another configured piece of information? Are there alternatives?</p>