spock.lang
Class Specification

java.lang.Object
  extended by spock.lang.Specification

public abstract class Specification
extends Object

Base class for Spock specifications. All specifications must inherit from this class, either directly or indirectly.


Field Summary
static Object _
          The wildcard symbol.
 
Constructor Summary
Specification()
           
 
Method Summary
 void interaction(Closure block)
          Encloses one or more interaction definitions in a then block.
 Object Mock()
          Creates a mock object whose name and type are inferred from the variable that the mock object is assigned to.
<T> T
Mock(Class<T> type)
          Creates a mock object of the given type.
 void noExceptionThrown()
          Specifies that no exception should be thrown.
 void notThrown(Class<? extends Throwable> type)
          Specifies that in particular, no exception of the given type should be thrown.
<T> T
old(T expression)
          Used in a then-block to access an expression's value at the time just before the previous where-block was entered.
<T extends Throwable>
T
thrown()
          Specifies that the preceding when block should throw an exception.
<T extends Throwable>
T
thrown(Class<T> type)
          Specifies that the preceding when block should throw an exception of the given type.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

_

public static final Object _
The wildcard symbol. Used in several places as a don't care value:

Constructor Detail

Specification

public Specification()
Method Detail

thrown

public <T extends Throwable> T thrown()
Specifies that the preceding when block should throw an exception. May only occur as the initializer expression of a typed variable declaration in a then block; the expected exception type is inferred from the variable type.

This form of exception condition is typically used if the thrown exception instance is used in subsequent conditions.

Example:

 when:
 "".charAt(0)

 then:
 IndexOutOfBoundsException e = thrown()
 e.message.contains(...)
 

Returns:
the thrown exception instance

thrown

public <T extends Throwable> T thrown(Class<T> type)
Specifies that the preceding when block should throw an exception of the given type. May only occur in a then block.

This form of exception condition is typically used if the thrown exception instance is not used in subsequent conditions.

Example:

 when:
 "".charAt(0)

 then:
 thrown(IndexOutOfBoundsException)

Type Parameters:
T - the expected exception type
Parameters:
type - the expected exception type
Returns:
the thrown exception instance

notThrown

public void notThrown(Class<? extends Throwable> type)
Specifies that in particular, no exception of the given type should be thrown. This method has only documentation purposes and does not affect the execution of the specification.

Parameters:
type - an exception type

noExceptionThrown

public void noExceptionThrown()
Specifies that no exception should be thrown. Equivalent to notThrown(Throwable). This method has only documentation purposes and does not affect the execution of the specification.


Mock

public Object Mock()
Creates a mock object whose name and type are inferred from the variable that the mock object is assigned to. For example, IOrderService service = Mock() will create a mock object named "service" and of type IOrderService.

Returns:
the new mock object

Mock

public <T> T Mock(Class<T> type)
Creates a mock object of the given type. If this method is used to initialize a new variable, the mock's name is inferred from the variable's name. For example, def service = Mock(IOrderService) will create a mock object named "service" and of type IOrderService. Otherwise, the mock will be named after its type (e.g. "IOrderService").

Type Parameters:
T - the type of the mock object to be created
Parameters:
type - the type of the mock object to be created
Returns:
the new mock object

interaction

public void interaction(Closure block)
Encloses one or more interaction definitions in a then block. Required when an interaction definition uses a statement that doesn't match one of the following patterns, and therefore isn't automatically recognized as belonging to an interaction definition:

Regular interaction definition:

 def "published messages are received at least once"() {
   when:
   publisher.send(msg)

   then:
   (1.._) * subscriber.receive(msg)
 }
 

Equivalent definition that uses a helper variable:

 def "published messages are received at least once"() {
   when:
   publisher.send(msg)

   then:
   interaction {
     def num = (1.._)
     num * subscriber.receive(msg)
   }
 }
 

Equivalent definition that uses a helper method:

 def "published messages are received at least once"() {
   when:
   publisher.send(msg)

   then:
   interaction {
     messageReceived(msg)
   }
 }

 def messageReceived(msg) {
   (1.._) * subscriber.receive(msg)
 }
 

Parameters:
block - a block of code containing one or more interaction definitions

old

public <T> T old(T expression)
Used in a then-block to access an expression's value at the time just before the previous where-block was entered.

Type Parameters:
T - the expression's type
Parameters:
expression - an arbitrary expression, except that it may not reference variables defined in the then-block
Returns:
the expression's value at the time the previous where-block was entered