title: How to exclude a Spring Boot Component from running in JUnit unit tests?
body: <blockquote>
  <p>How to exclude a Spring Boot Component from running in JUnit unit tests?</p>
  </blockquote>
  <p><strong>What I've tried</strong></p>
  <p>Please correct me if I'm wrong. <code>@ComponantScan</code> is excluding a class globally, both in main &amp; test?</p>
  <p>Since I don't want exclude my it globally, <strong>only during unit tests</strong>, is there any way to do that?</p>
  <pre><code>@Component
  public class DatabaseInitialization {
  
      @Autowired
      private UserRepository userRepository;
  
      @PostConstruct
      private void postConstruct() {
  
          User user = new User();
          user.setEmail(&quot;app@test.com&quot;);
          user.setPassword(&quot;1234567&quot;);
          user.setFirstName(&quot;Charle&quot;);
          user.setLastName(&quot;P.&quot;);
          user.setIbanCode(2641874);
          user.setBicCode(63215472);
          user.setFriendsList(&quot;[]&quot;);
  
          userRepository.save(user);
      }
  }
  </code></pre>