如何使用jUnit和Spring加载多个上下文

时间:2013-11-18 16:50:04

标签: java spring junit

我有一个带有以下注释的junit测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:custom-context.xml")

在我自己测试的测试中需要另一个上下文中的类,所以我需要加载这个上下文。如何实施?

我试过了:

@ContextConfiguration("classpath:custom-context.xml , classpath:custom-context2.xml")

但这现在可以正常工作,因为它似乎没有使用,分隔符加载多个上下文。

2 个答案:

答案 0 :(得分:3)

也可以使用这样的通配符:

@ContextConfiguration({"classpath:custom-*.xml" })

答案 1 :(得分:2)

使用字符串数组,例如:

@ContextConfiguration(locations={ "classpath:custom-context.xml" , "classpath:custom-context2.xml" })

来自spring api doc