为什么我的测试不能从其父级继承其ContextConfiguration位置?

Eva*_*aas 8 java junit spring spring-test

为了DRY的利益,我想在父类中定义我的ContextConfiguration并让我的所有测试类继承它,如下所示:

家长班:

package org.my;

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

}
Run Code Online (Sandbox Code Playgroud)

儿童班:

package org.my;

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

    @Inject
    private Foo myFoo;

    @Test
    public void myTest() {
          ...
    }
}
Run Code Online (Sandbox Code Playgroud)

根据ContextConfiguration文档,我应该能够继承父级的位置,但我无法让它工作.当Spring /org/my/ChildTest-context.xml找不到它时,Spring仍在默认位置()和barfs中查找文件.我试过以下没有运气:

  • 使父类具体化
  • 将无操作测试添加到父类
  • 将注入的成员添加到父类
  • 以上的组合

我正在使用spring-test 3.0.7和JUnit 4.8.2.

Jea*_*ond 13

删除@ContextConfiguration(inheritLocations = true)子类.inheritLocations默认情况下设置为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() {
          ...
    }
}
Run Code Online (Sandbox Code Playgroud)