Annotation Type Ensures


Represents a method postcondition.

A postcondition is a condition that is guaranteed to be fulfilled by suppliers.

A method's postcondition is executed after a method call has finished. A successor's postcondition strengthens the postcondition of its parent class, e.g. if A.someMethod declares a postcondition and B.someMethod overrides the method the postconditions are combined with a boolean AND.

Compared to pre-conditions, postcondition annotation closures are optionally called with two additional closure arguments: result and old.

result is available if the corresponding method has a non-void return-type and holds the result of the method call. Be aware that modifying the internal state of a reference type can lead to side-effects. Groovy-contracts does not keep track of any sort of modifications, neither any conversion to immutability.

old is available in every postcondition. It is a Map which holds the values of value types and Cloneable types before the method has been executed.

Examples:

Accessing the result closure parameter:

   @Ensures({ result -> result != argument1 })
   def T someOperation(def argument1, def argument2)  {
     ...
   }
 

Accessing the old closure parameter:

   @Ensures({ old -> old.counter + 1 == counter })
   def T someOperation(def argument1, def argument2)  {
     ...
   }
 

  • Required Element Summary

    Required Elements
    Modifier and Type
    Required Element
    Description
     
  • Element Details