In my spring boot application I have a bean specified:
package com.kiteautotrade.autotrader;
@SpringBootApplication
@Configuration
@EnableAsync
@ComponentScan(basePackages = {"com.gmailclient", "com.kiteautotrade.autotrader"})
public class AutotraderApplication implements CommandLineRunner {
. . .
@Bean
public Clock getClock() {
return Clock.system(ZoneId.of("Asia/Kolkata"));
}
}
In the spring boot test I try to provide a different bean:
package com.kiteautotrade.autotrader.integration;
. . .
@ExtendWith(SpringExtension.class)
@RunWith(SpringRunner.class)
@SpringBootTest(classes = AutotraderApplication.class,
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class IntegrationTest {
@Configuration
@ComponentScan(basePackages = {"com.gmailclient", "com.kiteautotrade.autotrader"})
static class MyTestConfiguration {
@Bean
@Primary
public Clock getClock() {
return Clock.fixed(
LocalDateTime.of(2023, 4, 25, 9, 30)
.toInstant(ZoneOffset.ofHoursMinutes(5, 30)), ZONE_ID);
}
}
However the bean is not getting overridden. What am I doing wrong? I have also treid adding @TestConiguration instead of / in addition to @Configuration in the static class MyTestConfiguration above, but it didn't make any difference. It appears like the application class's @Configuration takes effect first since its package is in the component scan of the test, but I need to override the Bean with a different one for the test. What am I doing wrong?