Groovy 使用@ContextHierarchy 测试class

Groovy test class using @ContextHierarchy

我正在为我的 Spring 应用程序使用 Groovy,并且我尝试在我的测试中使用多个 XML beans 配置。我尝试使用 @ContextHierarchy 但以下用法示例不起作用:

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

...

}

我也试过:

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

但效果不佳。

据我了解Groovy不喜欢“{”“}”,因为它有不同的含义....?

如何在 groovy 中编写定义了两个配置 xml 的测试类?

您可以使用 @ContextConfiguration 注释定义多个 XML 配置源。假设我有 2 个 XML 配置文件位于 src/main/resources - beans1.xmlbeans2.xml。我可以在我的测试中使用它们:

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

与Java相比的主要区别在于Groovy对数组使用[]而不是Java的{},因为{}代表Groovy的闭包。