0

我们有使用元注释的测试类:

@WebAppConfiguration
@ContextHierarchy({
    @ContextConfiguration(locations = {"/web/WEB-INF/spring.xml" }, name = "parent"),
    @ContextConfiguration("/web/WEB-INF/spring-servlet.xml")
})
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
public @interface BaseSpringTest {
}

但希望能够覆盖或附加到测试类本身的层次结构元素,例如:

@BaseSpringTest
@ContextConfiguration(locations = {"/web/WEB-INF/spring-extension.xml" }, name = "parent")
public class MyTest extends AbstractTestNGSpringContextTests {
    ...
}

到目前为止,这对我们没有用......是否有任何机制来实现这一点?我找到了https://jira.spring.io/browse/SPR-11038,但我认为这不是解决这种情况的方法。

谢谢!

4

1 回答 1

1

是否有任何机制来实现这一点?

不,没有支持这种配置风格的机制。

可以使用自定义组合注解代替实际注解,而不是与实际注解结合使用。在整个核心 Spring 框架中都是如此(也许@Profile和除外@Conditional)。

换句话说,您不能在同一个类上声明@ContextConfiguration和另一个使用元注释@ContextConfiguration(例如 your )的注释。@BaseSpringTest如果你这样做了,你会发现 Spring 只找到其中​​一个声明。

但是,如果您引入一个基类,您可以实现您的目标(尽管需要扩展该基类):

@BaseSpringTest
public abstract class AbstractBaseTests extends AbstractTestNGSpringContextTests {
    // ...
}

@ContextConfiguration(locations = {"/web/WEB-INF/spring-extension.xml" }, name = "parent")
public class MyTest extends AbstractBaseTests {
    // ...
}

当然,如果您采用“基类”路线,自定义组合注释可能对您没有那么有用。

问候,

Sam(Spring TestContext 框架的作者)

于 2014-08-02T21:02:43.500 回答