Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

spring

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!

like image 980
user3729326 Avatar asked Apr 21 '26 01:04

user3729326


1 Answers

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

  • Testing with @Configuration Classes and Profiles
  • Import annotation JavaDoc
  • ImportResource annotation JavaDoc
like image 135
Bohuslav Burghardt Avatar answered Apr 23 '26 00:04

Bohuslav Burghardt