为Junit中的继承Spring上下文提供自定义位置

时间:2016-02-09 11:39:12

标签: java spring junit

我有一个Spring应用程序和一个这样的基础测试类:

@RunWith(SpringJUnit4ClassRunner.class)
@ContextHierarchy({
    @ContextConfiguration(name = "parent", locations = "/app-context.xml"),
    @ContextConfiguration(name = "child", locations = "/employee-context.xml")
})
public class EmployeeTests{
//...
}

和儿童测试课

@ContextHierarchy(
    @ContextConfiguration(name = "child", locations = "/manager-context.xml")
)
public class ManagerTests extends EmployeeTests{

}

但这似乎加载了employee-context.xml和manager-context.xml的配置 我希望employee-context.xml配置在ManagerTests中没有任何效果,但是要在从EmployeeTests创建子类的其他类中生效

1 个答案:

答案 0 :(得分:0)

您可以保留EmployeeTests,并将此行添加到ManagerTests

@ContextHierarchy(
@ContextConfiguration(name = "child",
locations = "/manager-context.xml",
 inheritLocations = false //add this line to your code
))
public class ManagerTests extends EmployeeTests{

}