Question

We have test classes using a meta-annotation:

@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 {
}

But would like to be able to override, or append to elements of the hierarchy from the test class itself, eg:

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

This hasn't worked for us so far... is there any mechanism in place to make this happen? I found https://jira.spring.io/browse/SPR-11038, but I don't think that is quite the fix for this type of situation.

Thanks!

Was it helpful?

Solution

is there any mechanism in place to make this happen?

No, there is no such mechanism that supports this style of configuration.

Custom composed annotations can be used instead of the actual annotations, not in conjunction with the actual annotations. This is true across the core Spring Framework (with the exception of perhaps @Profile and @Conditional).

In other words, you can't declare @ContextConfiguration and another annotation that is meta-annotated with @ContextConfiguration (e.g., your @BaseSpringTest) on the same class. If you do, you'll find that Spring only finds one of those declarations.

However, if you introduce a base class you can achieve your goal (albeit with the requirement to extend that base class):

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

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

Of course, if you're going the 'base class' route, the custom composed annotation is perhaps not as useful to you.

Regards,

Sam (author of the Spring TestContext Framework)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top