title: How to mock a class that been annotated with Primary
body: <p>I have :</p>
  
  <ul>
  <li>an interface : <code>EntityService</code></li>
  <li>a first implementation : <code>EntityServiceImpl</code> - This class is annotated with <code>@Primary</code></li>
  <li>an other one : <code>EntityServiceClientImpl</code></li>
  <li>and a controller that has this field <code>@Autowired EntityService</code></li>
  </ul>
  
  <p>I would like to do a test on this controller and for this test to be unitary I mock <code>EntityService</code>.</p>
  
  <p>So of course this code does not work because Spring detects two beans annotated with Primary :</p>
  
  <pre><code>@Configuration
  class EntityControllerTestConfig {
      @Bean
      @Primary
      EntityService entityService() {
          return mock(EntityService.class);
      }
  }
  
  @RunWith(SpringRunner.class)
  @SpringBootTest(classes = TestApplication.class)
  @WebAppConfiguration
  @ContextConfiguration(classes = EntityControllerTestConfig.class)
  public class EntityControllerTest {
  
      @Autowired
      private EntityService entityService;
  </code></pre>
  
  <p>...</p>
  
  <pre><code>@SpringBootApplication(scanBasePackages= "com.company.app")
  @EntityScan (basePackages = {"com.company.app" }, basePackageClasses = {Jsr310JpaConverters.class })
  @EnableJpaRepositories("com.company.app")
  public class TestApplication {
      public static void main(String[] args) {
          SpringApplication.run(TestApplication.class, args);
      }
  }
  </code></pre>
  
  <p>I tried to find an other way to mock and to exclude <code>EntityServiceClient</code> on test configuration but i was not able to mock. (cf : <a href="https://stackoverflow.com/questions/18992880/exclude-component-from-componentscan">exclude @Component from @ComponentScan</a> )</p>