【发布时间】:2020-06-25 20:31:12
【问题描述】:
我有一个注释,
@Retention(RetentionPolicy.RUNTIME)
@Target(value={ElementType.METHOD, ElementType.TYPE})
public @interface JobLoggable {
JobType jobType();
JobDomain domain();
JobStatus jobInitialStatus();
}
我适用于我的班级
@JobLoggable
public class Process {
public void doSomething() {
Annotation[] annotations = this.getClass().getAnnotations();
Assert.assert(annotations.length == 1);
}
}
在我的测试中,我监视它(不是模拟它,尽管这无关紧要):
public class Test {
@Test
public void test() {
Process process = Mockito.spy(Process.class);
//spy on some internal methods, capture their return values
process.doSomething();
}
}
遗憾的是 this.getClass().getAnnotations() 没有返回任何注释。当我查看实例是什么类时,它是: Process$$EnhancerByMockitoWithCGLIB$$3db5579b
它不是真正的 Process 类。我想这就是它无法检索注释的原因。
也许这有一些技巧。有任何想法吗?谢谢大家。
我已经尝试在流程实例上模拟 getClass() 但它是最终的,因此无法模拟。
【问题讨论】:
标签: annotations mockito java-annotations