I have two implementations for a given interface both are annotated with @Component.
One implementation is in src/main -> IImplementationCore.java Other implementation is in src/test -> IImplementationTest.java
I have a test class ABCTest.java in src/test which is annotated with
@RunWith(SpringRunner.class), @SpringBootTest, @ContextConfiguration, @ComponentScan, @EnableAutoConfiguration
I want this ABCTest.java to inject IImplementationCore while running the test. But it is injecting IImplementationTest.java.
package structure:
- src
- main
- package
- IImplementationCore.java
- package
- test
- package
- IImplementationTest.java
- package2
- ABCTest.java
- package
- main
Solution I know:
- Annotating @Primary for IImplementationCore.java resolves the issue.
I somehow do not like to force something as primary because it is being annotated as primary due to some bean conflict from src/test but not due to some real conflict in core implementations. what are my other options? What I am looking for is, is there a way to only consider beans from src/main but not from src/test? Or is there any better way?