I have a problem with overriding beans in integration tests in Spring (with Spock).
Let's say this is my application config:
@EnableWebMvc
@SpringBootApplication
@Configuration
class Main {
@Bean
Race race(Car car) {
// ...
}
@Bean
Car car() {
// ...
}
}
And I have 2 separate integration tests that I want to have to separate Car
implementations provided.
@Slf4j
@SpringApplicationConfiguration
class OneIntegrationSpec extends AbstractIntegrationSpec {
@Configuration
@Import(Main.class)
static class Config {
@Bean
Car oneTestCar() {
return new FerrariCar();
}
}
}
@Slf4j
@SpringApplicationConfiguration
class OtherIntegrationSpec extends AbstractIntegrationSpec {
@Configuration
@Import(Main.class)
static class Config {
@Bean
Car otherTestCar() {
return new TeslaCar();
}
}
}
When I run one of these I am getting: NoUniqueBeanDefinitionException
cause Spring detects there are multiple car implementations.
How to make test inner class Config
with @Configuration
annotation being loaded only for that particular test?
I saw the approach with @Profile
but that would mean creating separate profiles names for each IntegrationSpec
which is a little bit violating a DRY. Is there another approach than @ActiveProfiles
?