jd466
jd466

Reputation: 579

Defining embeded annotation interface in Java

I have following annotation interface:

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface MockToken {

    String user() default "user";

    String[] allowedConfigs() default {};

}

Within this annotation, I would like to define another annotation and initalize it in the test. The data should be an array at the end:

  "permissions": [
        {
            "resource_id": "xy",
            "resource_scopes": [
                "download"
            ],
            "exp": 1522334692
        }
    ]

And I would like to initialize them in the test:

@Test
@MockToken(user= "user1", allowedConfigs = {testUser}, permission = examplePermission)

My problem is now how to define permission annotation and how to define examplePermission in the test.

My goal is at the end to have field access like this: mockToken.permissions().resourceId();

Upvotes: 0

Views: 52

Answers (1)

Harukero
Harukero

Reputation: 11

Unfortunately, the types of attribute you can use in an annotation are limited.

Maybe you can directly try to inject the permission object itself as a parameter for your test? In that case, you'll need a ParameterResolver.

In this link they explain this quite well: https://www.baeldung.com/junit-5-extensions#4-parameter-resolution

You could also consider creating your own junit extension.

You can also try to create a @ParameterizedTest?

Upvotes: 1

Related Questions