Java 为什么可以';我的测试是否从其父级继承其ContextConfiguration位置?

Java 为什么可以';我的测试是否从其父级继承其ContextConfiguration位置?,java,spring,junit,spring-test,Java,Spring,Junit,Spring Test,出于DRY的兴趣,我想在父类中定义ContextConfiguration,并让所有测试类继承它,如下所示: 父类: package org.my; @RunWith(SpringJUnit4ClassRunner.class) @ContextConfiguration(locations = "/org/my/Tests-context.xml") public abstract class BaseTest { } 儿童班: package org.my; @RunWith(Spr

出于DRY的兴趣,我想在父类中定义ContextConfiguration,并让所有测试类继承它,如下所示:

父类:

package org.my;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "/org/my/Tests-context.xml")
public abstract class BaseTest {

}
儿童班:

package org.my;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(inheritLocations = true)
public class ChildTest extends BaseTest {

    @Inject
    private Foo myFoo;

    @Test
    public void myTest() {
          ...
    }
}
根据文档,我应该能够继承父对象的位置,但我无法让它工作。Spring仍在默认位置(
/org/my/ChildTest context.xml
)中查找文件,当找不到时会发出barfs。我尝试了以下方法,但没有成功:

  • 使父类具体化
  • 向父类添加无操作测试
  • 同时向父类添加注入成员
  • 上述各项的组合

我正在进行spring测试3.0.7和JUnit 4.8.2。

删除子类上的
@ContextConfiguration(inheritLocations=true)
<代码>继承位置默认设置为true

通过添加
@ContextConfiguration(inheritLocations=true)
注释而不指定位置,您告诉Spring通过添加默认上下文(即
/org/my/ChildTest context.xml
来扩展资源位置列表

试试这样的方法:

package org.my;

@RunWith(SpringJUnit4ClassRunner.class)
public class ChildTest extends BaseTest {

    @Inject
    private Foo myFoo;

    @Test
    public void myTest() {
          ...
    }
}

就这样!这提醒了我,我需要去接受你的另一个答案。你正式成为本周的最有价值球员:-)