junit 5 custom parametrized tests

356 views Asked by At

I'm using Junit 5 parametrized tests with custom name as follow

    @ParameterizedTest(name = PARAMETERIZED_TESTS_NAME_PLACEHOLDER)

where PARAMETERIZED_TESTS_NAME_PLACEHOLDER is defined in its own utility class

public static final String PARAMETERIZED_TESTS_NAME_PLACEHOLDER = "#{index} [{argumentsWithNames}]";

the problem I'm facing is that as I'm using extensively the parametrized tests, my code is cluttered by these @ParameterizedTest(name = PARAMETERIZED_TESTS_NAME_PLACEHOLDER).

so I created a custom annotation to fix this

import java.lang.annotation.*;
import org.junit.jupiter.params.*;

@ParameterizedTest(name = PARAMETERIZED_TESTS_NAME_PLACEHOLDER)
@Inherited
public @interface CustomParametrizedTest {

}

but this annotation is ignored when I use it in the test cases

any help would be appreciated

1

There are 1 answers

1
Sam Gregory On BEST ANSWER

The @ParamterizedTest annotation appears to have a retention policy of runtime suggesting it's needed and processed at runtime. Try this config

@ParameterizedTest(name = PARAMETERIZED_TESTS_NAME_PLACEHOLDER)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CustomParametrizedTest {

}

It seems odd to me that this is not the default retention policy for custom annotations, see more from this post.