在Spring中,ContextConfiguration(...)是否从其父@ContextConfiguration继承?

haw*_*eye 7 java junit spring spring-mvc

假设我有一个父测试类,如下所示:

@ContextConfiguration(loader = AnnotationConfigContextLoader.class, classes = { MyCustomTestConfig.class })
public class MyParentTestclass {
Run Code Online (Sandbox Code Playgroud)

然后我有一个子类,我想在其中添加Spring 3.2.3名称注释属性.

@ContextConfiguration(name=MyName)
public class MyChildTestClass extends MyParentTestClass {
Run Code Online (Sandbox Code Playgroud)

我仍然希望从父级获取所有上下文配置 - 但我不确定它是否会通过.

我的问题是:在Spring中,ContextConfiguration(...)是否从其父@ContextConfiguration继承?

pcz*_*eus 5

在@ContextConfiguration 不支持继承开箱。

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

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

如果inheritLocations 设置为false,则资源位置或注释类的注释类将隐藏并有效替换超类定义的任何资源位置或注释类。

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

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

 @ContextConfiguration(classes=ExtendedConfig.class)
 public class ExtendedTest extends BaseTest {
     // ...
 }
Run Code Online (Sandbox Code Playgroud)