[英]How to make sure the @Scheduled annotation is present on the method
在我们的Spring Boot
应用程序中,我们有一个带有@Scheduled
批注的方法:
@Scheduled(fixedRate = 1000L * 60 * 10, initialDelay = 1000L * 60 * 5)
public void doSomethingFromTimeToTime(){....}
在我们的Maven构建测试阶段,我们希望确保:
有没有办法在单元/集成测试中对此进行测试? 还是在构建阶段用另一种方式?
我们不需要测试Spring的Scheduler本身,只需确保注释在那里。
可以测试的注释存在一个上Class
使用对象.getAnnotation(Class<A> annotationType)
所述的方法Class
,其中A
是一个通用的类型参数,正如T
通常是。
用法示例:
Scheduled scheduledAnnotation = MySpringScheduler.class.getAnnotation(Scheduled.class).
有了注释后,就可以像使用普通类一样使用注释提供的方法访问字段:
scheduledAnnotation.fixedDelay();
我想通过这种方式您可以使用反射轻松获得它
Class<?> cls = Class.forName(name);
cls.getDeclaredAnnotations(); // get all annotation
(cls.getDeclaredMethods()[0]).getAnnotations(); //get annotation of a method
Annotation ety = cls.getAnnotation(Annotation.class); // get annotation of particular annotation class
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.