简体   繁体   English

使用其他注释的Java自定义注释

[英]Java Custom Annotation using another annotation

Because of problems with Powermock and Java 11, we have had to use the @PowerMockIgnore on all our test classes. 由于Powermock和Java 11的问题,我们不得不在所有测试类上使用@PowerMockIgnore。

@PowerMockIgnore({ "javax.management.*", "com.sun.org.apache.xerces.*", "javax.xml.*",
        "org.xml.*", "org.w3c.dom.*", "com.sun.org.apache.xalan.*", "javax.activation.*" })

In order to avoid duplicating this in all test classes, we created a custom annotation to be used instead that had this PowerMockIgnore defined on one place. 为了避免在所有测试类中重复此操作,我们创建了一个自定义批注来代替,该批注在一个位置定义了此PowerMockIgnore。 So that all we would need in all our test classes would be 这样我们所有测试类中所需的就是

@SuppressPowerMockInitError

However: This custom annotation does not seem to work. 但是:此自定义注释似乎不起作用。

import static java.lang.annotation.ElementType.METHOD;
import static java.lang.annotation.ElementType.TYPE;
import static java.lang.annotation.RetentionPolicy.RUNTIME;

import java.lang.annotation.Documented;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.Target;

import org.powermock.core.classloader.annotations.PowerMockIgnore;

@Retention(RUNTIME)
@Target({ TYPE, METHOD })
@Inherited
@Documented
@PowerMockIgnore({ "javax.management.*", "com.sun.org.apache.xerces.*", "javax.xml.*",
        "org.xml.*", "org.w3c.dom.*", "com.sun.org.apache.xalan.*", "javax.activation.*" })
public @interface SuppressPowerMockInitError {

}

It does not look like it is possible to achieve this with annotations. 看起来不可能通过注释来实现。

However creating an abstract class that has this common annotation seems like the best way to achieve this. 但是,创建具有此公共注释的抽象类似乎是实现此目的的最佳方法。

import org.powermock.core.classloader.annotations.PowerMockIgnore;

@PowerMockIgnore({ "javax.management.*", "com.sun.org.apache.xerces.*", "javax.xml.*",
        "org.xml.*", "org.w3c.dom.*", "com.sun.org.apache.xalan.*", "javax.activation.*" })
public abstract class AbstractPowerMockTest {

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM