junit 5自定义参数化测试

huangapple go评论94阅读模式
英文:

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.

huangapple
  • 本文由 发表于 2020年10月9日 17:22:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/64277236.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定