title: How to test Spring @Conditional beans
body: <p>I have a @Conditional bean - </p>
  
  <pre><code>@RestController("/user")
  @ConditionalOnProperty(prefix = "user-controller", name = "enabled", havingValue = "true")
  public void UserController {
  
  @GetMapping
  public String greetings() {
    return "Hello User";
  }
  
  }
  </code></pre>
  
  <p>it can be either enabled or disabled. I want to create a test to cover both use cases. How can I do that? I just have one <strong>application.properties</strong> file:</p>
  
  <pre><code>user-controller.enabled=true
  </code></pre>
  
  <p>I can inject the property into bean and add a setter to manage it via code, but that solution is not elegant:</p>
  
  <pre><code>@RestController("/user")
  @ConditionalOnProperty(prefix = "user-controller", name = "enabled", havingValue = "true")
  public void UserController {
  
  @Value("${user-controller.enabled}")
  private boolean enabled;
  
  public void setEnabled(boolean enabled) {
   this.enabled = enabled;
  }
  
  @GetMapping
  public String greetings() {
    return enabled ? "Hello User" : "Endpoint is disabled";
  }
  
  }
  </code></pre>
  
  <p>smth like this</p>