gpt4 book ai didi

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

转载 作者:太空宇宙 更新时间:2023-11-04 12:57:10 25 4
gpt4 key购买 nike

我有一个 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子类化的其他类中起作用

最佳答案

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

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

}

关于java - 为 Junit 中继承的 Spring 上下文提供自定义位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35291116/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com