user3729326
user3729326

Reputation: 55

@ContextConfiguration how to use XML based config and Java-based at same time?

I'm writing integration tests with SpringJUnit4. I got question. How in @ContextConfiguration I can use XML based config and Java-based at same time. As I know I couldn't do it, but maybe there exist backdoor? Thanks in advance!

Upvotes: 3

Views: 3285

Answers (2)

Bohuslav Burghardt
Bohuslav Burghardt

Reputation: 34776

You could create static inner @Configuration class in your test class and use @ContextConfiguration annotation on your class without any parameters. As stated in the article below, Spring will automatically look for static inner @Configuration class if no XML locations or config classes are passed to the annotation.

You can then import your XML config and Java config classes using @Import and @ImportResource annotations. So your base class for your Spring tests could look something like this:

@ContextConfiguration
@RunWith(SpringJUnit4ClassRunner.class)
public class BaseSpringTest {
    @Configuration
    @Import(BaseConfig.class)
    @ImportResource({ "classpath:applicationContext-hibernate.xml" })
    public static class ContextConfig {}
}

Sources

Upvotes: 10

Slava Semushin
Slava Semushin

Reputation: 15204

Use @ImportResource on @Configuration class to import XML based config.

Upvotes: 0

Related Questions