2016-03-24 28 views
3

假设我有一个父测试类,像这样:在Spring中,ContextConfiguration(...)从它的父@ContextConfiguration继承吗?

@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { MyCustomTestConfig.class }) 
public class MyParentTestclass { 

然后,我有一个子类,我想添加Spring 3.2.3 name annotation attribute

@ContextConfiguration(name=MyName) 
public class MyChildTestClass extends MyParentTestClass { 

我还想从父级获取所有上下文配置 - 但不知道是否会通过。

我的问题是:在春天做了ContextConfiguration(...)从其父继承@ContextConfiguration?

回答

1

@ContextConfiguration确实支持开箱即用的继承。

@ContextConfiguration有一个名为inheritLocations的属性,默认为true并指示是否应继承来自测试超类的资源位置或注释类。

将inheritLocations =真:这意味着一个注解的类将继承由测试超类中定义的资源位置或注解的类。具体而言,给定测试类的资源位置或注释类将被附加到由测试超类定义的资源位置或注释类的列表中。因此,子类可以选择扩展资源位置或注释类的列表。

如果将inheritLocations设置为false,对父类的资源位置或注解的类将阴影和有效替代任何资源位置或超类定义注释类。

在以下使用带注释类的示例中,ExtendedContext的ApplicationContext将从BaseConfig和ExtendedConfig配置类中按此顺序加载。因此,在ExtendedConfig中定义的Bean可能会覆盖在BaseConfig中定义的Bean。

@ContextConfiguration(classes=BaseConfig.class) 
public class BaseTest { 
    // ... 
} 

@ContextConfiguration(classes=ExtendedConfig.class) 
public class ExtendedTest extends BaseTest { 
    // ... 
} 
相关问题