【问题标题】:ContextConfiguration inheritance in junitjunit中的ContextConfiguration继承
【发布时间】:2015-04-28 15:17:32
【问题描述】:

我正在尝试模拟一个从 redis 获取数据的服务。我已经在新的上下文文件 test-context.xml 中注入了 spring 上下文中的 bean 但我有其他上下文文件 A.xml,B.xml 引用test-context.xml中beanS中的方法。我在enter link description here中阅读了问题

它创建了一个 BaseTest 类,但是当我继承该类时,我的子类中的上下文文件首先被加载,而不是它应该首先加载基类的上下文文件,因为子类上下文中的 bean 依赖于基类上下文。

 @ContextConfiguration(locations = { "/META-INF/spring/testContext.xml" }) 
 public abstract class myBaseTest { 
 @Before
 public void init() {
    // custom initialization code for resources loaded by testContext.xml 
 }

 @After
public void cleanup() {
    // custom cleanup code for resources loaded by testContext.xml
}
}

 @RunWith(SpringJUnit4ClassRunner.class)
 @ContextConfiguration(locations = { "/META-INF/spring/A.xml",
                                 "/META-  INF/spring/B.xml" }) 
 public class childTest extends myBaseTest { ... }

【问题讨论】:

标签: spring junit easymock


【解决方案1】:

您可以简单地将父上下文添加到子配置中。

 @ContextConfiguration(locations = { "/META-INF/spring/testContext.xml",
                             "/META-INF/spring/A.xml",
                             "/META-INF/spring/B.xml"}) 

如果您不覆盖 @Before 和 @After 方法,它们将正常工作。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    相关资源
    最近更新 更多