I've a bean which is created as follow
@Profile({"test", "dev", "int"})
@Bean
public CustomerEmailSenderImpl customerEmailSenderImpl(){
return new CustomerEmailSenderImpl ();
}
And in a test class, I mock the class as follow:
@ActiveProfiles(profiles = {"test"})
.....
@MockBean
private CustomerEmailSenderImpl customerEmailSenderImpl;
Now. I have to create a second email class which must be used specifically when with profile "test". So I created an Interface (CustomerEmailSender) which both classes implement. And the bean creation is done as follow.
@Profile({"dev", "int"})
@Bean(name = "customerEmailSender")
public CustomerEmailSender customerEmailSenderImpl1(){
return new CustomerEmailSenderImpl1 ();
}
@Profile({"test"})
@Bean(name = "customerEmailSender")
public CustomerEmailSender customerEmailSenderImpl2(){
return new CustomerEmailSenderImpl2 ();
}
The Mock I changed a follow
@ActiveProfiles(profiles = {"test"})
...
@MockBean
private CustomerEmailSender customerEmailSender;
The application starts without errors. But the test doesn't mock the bean CustomerEmailSenderImpl2. The bean is always instantiated, and the real code is executed. Even changing from Interface to Class-name in the test class didn't help:
@MockBean
private CustomerEmailSenderImpl2 customerEmailSenderImpl2;
What is needed to have the bean CustomerEmailSenderImpl2 mocked ?