1

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!

1 Answer 1

6

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 {};
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.