Can I use @Import in Spring JUnit tests?
I have mutual exclusive config configuration classes in my project: Config1
and Config2
, I want to select one in the unit test, like:
public class Config1 {
@Bean
FooBean foo() {
return new FooBean();
}
}
(I must remove the `@Configuration` annotation, so I can choose one in the application)
And,
@RunWith(SpringJUnit4ClassRunner.class)
@Import(Config1.class)
pu开发者_StackOverflowblic class FooTest {
@Inject
FooBean foo;
// ...
}
However, it seems like JUnit4ClassRunner
doesn't see the @Import
annotation on the test class.
From the Documentation of @Import:
Provides functionality equivalent to the {@literal <import/>} element in Spring XML.Only supported for actual {@literal @Configuration}-annotated classes.
So you are right, the annotation is ignored.
精彩评论