Search code examples
groovyspring-testcontext-configuration

Groovy test class using @ContextHierarchy


I am using Groovy for my Spring application and I try to use multiple XML beans configurations inside my test. I tried using @ContextHierarchy but following usage example didn't work:

@RunWith(SpringRunner)
@SpringBootTest
@ContextHierarchy({@ContextConfiguration("a.xml"), ContextConfiguration("b.xml")})
public class MyTest {

...

}

I have also tried:

@ContextConfiguration(locations={"a.xml", "b.xml"})

but it didn't work as well.

As I understand Groovy doesn't like the "{" "}", because it has different mean to it....?

How can I write the Testclass in groovy with two conifiguration xmls defined?


Solution

  • You can define multiple XML configuration sources with @ContextConfiguration annotation. Let's say I have 2 XML config files located in src/main/resources - beans1.xml and beans2.xml. I can use them in my test with:

    @ContextConfiguration(locations = ['classpath:beans1.xml', 'classpath:beans2.xml'])
    

    The main difference comparing to Java is that Groovy uses [] for arrays instead of Java's {}, because {} represents Groovy's closure.