且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将 JUnit 注释组合成自定义注释?

更新时间:2023-10-06 23:16:52

您可以制作一个 组合注释:

JUnit Jupiter 注释可以用作元注释.这意味着您可以定义自己的组合注解,该注解将自动继承元注解的语义.

JUnit Jupiter annotations can be used as meta-annotations. That means that you can define your own composed annotation that will automatically inherit the semantics of its meta-annotations.

例如:

import java.lang.annotation.Documented;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import org.junit.jupiter.api.Tag;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.ANNOTATION_TYPE, ElementType.METHOD})
@ParameterizedTest
@MethodSource("myorg.ccrtest.testlogic.DataProviders#standardDataProvider")
@Tag("ccr")
@Tag("standard")
public @interface CcrStandardTest {}

然后您将组合注释放在您的测试方法上:

You would then place the composed annotation on your test method:

@CcrStandardTest
void testFoo(/* necessary parameters */) {
  // perform test
}