2015-04-28 43 views
4

我想模拟一个服务,它从redis中获取数据。我已经在新的上下文文件test-context.xml中的spring上下文中注入了bean。但是我有其他上下文文件A.xml,B。 xml在test-context.xml中参考beanS中的方法。我在enter link description here中阅读了这个问题junit中的ContextConfiguration继承

它创建了一个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 { ... } 
+0

这不是你的问题的答案,但看看这篇文章,了解为什么不在测试系统中使用继承http://www.petrikainulainen.net/programming/unit-testing/3-reasons-why-we -should - 不使用继承功能于我们的测试/ – Vihar

回答

1

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

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

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