Lenik
Lenik

Reputation: 14458

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)
public class FooTest {
    @Inject
    FooBean foo;
    // ...
 }

However, it seems like JUnit4ClassRunner doesn't see the @Import annotation on the test class.

Upvotes: 1

Views: 1296

Answers (1)

Ralph
Ralph

Reputation: 120771

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.

Upvotes: 1

Related Questions