Annotation Type Use


  • @Retention(RUNTIME)
    @Target({TYPE,METHOD})
    @Repeatable(Container.class)
    public @interface Use
    Activates one or more Groovy categories while the annotated spec method or class executes. In other words, @Use(SomeCategory) has the same effect as wrapping the execution of the annotated method or class with use(SomeCategory) { ... }.

    Basic example:

     class ListExtensions {
       static avg(List list) { list.sum() / list.size() }
     }
    
     class MySpec extends Specification {
       @Use(ListExtensions)
       def "can use avg() method"() {
         expect:
         [1, 2, 3].avg() == 2
       }
     }
     

    One use case for this feature is the stubbing of dynamic methods which are usually provided by the runtime environment (e.g. Grails).

    Note: @Use has no effect when applied to a helper method. However, when applied to a spec class it will also affect its helper methods.

    Note: If this extension is applied on the Specification, then it will use ExecutionMode.SAME_THREAD for the whole Spec.

    • Required Element Summary

      Required Elements 
      Modifier and Type Required Element Description
      Class[] value  
    • Element Detail