英文:
junit 5 custom parametrized tests
问题
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
英文:
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
得分: 1
@ParamterizedTest
注解似乎具有保留策略为运行时,这表明它在运行时需要并且会在运行时进行处理。尝试这个配置
@ParameterizedTest(name = PARAMETERIZED_TESTS_NAME_PLACEHOLDER)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface CustomParametrizedTest {
}
我觉得奇怪的是,这不是自定义注解的默认保留策略,可以从这个帖子中了解更多信息。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论