How would you go about integration testing a spring application that is annotation-configured and component-scanned and does not have an XML configuration at all? I’m hitting a wall with the need to replace production components with testing components without actually resorting to xml configuration or reflection injections to all the @autowired parts.
Example:
interface A {...}
@Component
class AImpl implements A {
...
}
interface B {...}
@Component
class BImpl implements B {
@Autowired A a;
...
}
interface C {...}
class CImpl implements C {
@Autowired B b;
...
}
then in my test I want to use ATestImpl, but I only have access to C (integration testing C).
How would you go about doing that?
Take advantage of
@Primaryannotation:If there is more than one bean implementing
A, Spring will prefer the one annotated with@Primary. If you placeTestAclass in/src/test/java, it will only be picked up during test execution, on normal context startup Spring won’t seeTestAand use only avaialbleAImpl.