I have :
- an interface :
EntityService
- a first implementation :
EntityServiceImpl
- This class is annotated with@Primary
- an other one :
EntityServiceClientImpl
- and a controller that has this field
@Autowired EntityService
I would like to do a test on this controller and for this test to be unitary I mock EntityService
.
So of course this code does not work because Spring detects two beans annotated with Primary :
@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;
...
@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);
}
}
I tried to find an other way to mock and to exclude EntityServiceClient
on test configuration but i was not able to mock. (cf : exclude @Component from @ComponentScan )