Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/336.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何确保方法上存在@Scheduled注释_Java_Unit Testing_Spring Boot_Integration Testing_Scheduler - Fatal编程技术网

Java 如何确保方法上存在@Scheduled注释

Java 如何确保方法上存在@Scheduled注释,java,unit-testing,spring-boot,integration-testing,scheduler,Java,Unit Testing,Spring Boot,Integration Testing,Scheduler,在我们的Spring Boot应用程序中,我们有一个带有@Scheduled注释的方法: @Scheduled(fixedRate = 1000L * 60 * 10, initialDelay = 1000L * 60 * 5) public void doSomethingFromTimeToTime(){....} 在maven构建测试阶段,我们希望确保: 注释存在(例如,开发人员没有错误地删除注释)并且 包含这些精确值 有没有办法在单元/集成测试中测试这一点?还是在构建阶段的另一

在我们的
Spring Boot
应用程序中,我们有一个带有
@Scheduled
注释的方法:

@Scheduled(fixedRate = 1000L * 60 * 10, initialDelay = 1000L * 60 * 5)
   public void doSomethingFromTimeToTime(){....}
在maven构建测试阶段,我们希望确保:

  • 注释存在(例如,开发人员没有错误地删除注释)并且
  • 包含这些精确值
  • 有没有办法在单元/集成测试中测试这一点?还是在构建阶段的另一种方式


    我们不需要测试Spring的调度程序本身,只需要确保注释在那里。

    我想这样你就可以使用反射轻松地获得它

    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
    
    Class cls=Class.forName(name);
    cls.getDeclaredAnnotations();//获取所有注释
    (cls.getDeclaredMethods()[0]).getAnnotations()//获取方法的注释
    注释ety=cls.getAnnotation(Annotation.class);//获取特定注释类的注释
    

    ref

    您可以使用
    的方法测试
    对象上是否存在注释,其中
    a
    T
    通常一样是泛型类型参数

    用法示例:

    Scheduled scheduledAnnotation = MySpringScheduler.class.getAnnotation(Scheduled.class).
    
    拥有注释后,可以使用注释提供的方法访问字段,就像在普通类上一样:

    scheduledAnnotation.fixedDelay();
    

    嗨@riorio,你会有机会寻找我吗?一旦你有了
    注释
    对象,你就可以用提供的getter检查它的个人属性了。@PaulBenn这正是我需要的。为了荣誉,你想补充一个答案吗?很高兴我能帮忙。等一下,我会把它作为答案加上,这样其他人也能从中受益。