How to override @Configuation which is present under src/main/java with @TestConfiguration during unit tests?
@Configuration
public class AppConfig {
@Bean
public EmployeeService employeeService(){
return new EmployeeService();
}
}
@Component
public class ServerStartSetup implements CommandLineRunner {
@Autowired
private EmployeeService employeeService;
public void run(String... args) {
// do something with employee service
}
}
I would like to override the above bean with some below custom bean for testing purposes.
@TestConfiguration
public class TestAppConfig {
@Bean
public EmployeeService employeeService(){
return new FakeEmployeeService();
}
}
@SpringBootTest
@Import(TestAppConfig.class)
public class UnitTest {
}
However AppConfig does not seem to be skipped. That is , it throws an error saying that there is a bean with same name employeeService
. If I rename bean method name in the TestAppConfig
, it injects the bean created via AppConfig
.
How to fix this.?
Note: One possible solution is using @Profile
. I am looking for anything other than using Profiles.