Closed tuegeb closed 8 years ago
@Qualifier is already supported, but Spring's @Service/@Repository/@Component/@Controller/@Primary is not.
However, I believe a better solution will be to improve the support for @Tested object reuse. With that, the test class would be written as:
public class ClassWithQualifiedSpringDependencyTest {
@Tested(fullyInitialized = true) Dependency commonDependency;
@Tested(fullyInitialized = true) ClassWithQualifiedDependency tested;
@Test
public void testInjection() {
assertNotNull(tested);
assertNotNull(tested.getDependency());
assertSame(tested.getDependency().getClass(), Dependency.class);
}
}
... where the "commonDependency" tested object would be reused for the dependency in the other tested object. This already works, provided other classes implementing the interface haven't been loaded yet.
Thanks, that sounds good. It is not always possible to control which implementation classes will be loaded before the test execution: It would be helpful, if the preferred implementation class could optionally be set via an attribute in the annotation. Example:
@Tested(fullyInitialized = true,
preferredClass = Dependency.class)
IDependency commonDependency;
@Tested(fullyInitialized = true)
ClassWithQualitfiedDependency tested;
You will be able to use the implementation class (Dependency) as the type for the @Tested field, so this "preferredClass" attribute won't be necessary.
That is even better, as it is much more intuitive.
Many thanks for the implementation of this great enhancement.
Support constructor injection when using Spring with @Qualifier. Real (unmocked) instance of the type which corresponds to the chosen qualifier, should satisfy the constructor of the tested class.
Currently this test fails:
The following exception is thrown: