Is it possible to overwrite the parameter values of an annotation used in a custom annotation?
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest(classes = [HOW_TO_CHANGE_THESE])
@ComponentScan(basePackages = {[HOW_TO_CHANGE_THESE]})
@ActiveProfiles("testing")
public @interface IntegrationTest {
}
My intention was to reduce the number of annotations in my tests, while setting default values to all tests of this type. I would like to use it like this:
@IntegrationTest(classes="...", basePackages="...")
class AbcTest {
}
Thanks in advance!
CodePudding user response:
Yes, it is possible if you are using the Spring framework. It has a special @AliasFor annotation for this purpose:
@AliasFor is an annotation that is used to declare aliases for annotation attributes.
You can find more examples of its usage in the official documentation here or in this tutorial.
Also, there are details of how to use meta-annotations in the Spring Test Context in the official documentation here.
Your example with the usage of this annotation will look like this:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@SpringBootTest
@ComponentScan
@ActiveProfiles("testing")
public @interface IntegrationTest {
@AliasFor(annotation = ComponentScan.class, attribute = "basePackages")
String[] basePackages() default {};
@AliasFor(annotation = SpringBootTest.class, attribute = "classes")
Class<?>[] classes() default {};
}