3

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!

2 Answers 2

10

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

Sign up to request clarification or add additional context in comments.

Comments

0

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.