Package spock.lang
Class Specification
- java.lang.Object
-
- org.spockframework.lang.SpecInternals
-
- spock.mock.MockingApi
-
- spock.lang.Specification
-
- All Implemented Interfaces:
MockFactory
@Testable public abstract class Specification extends MockingApi
Base class for Spock specifications. All specifications must inherit from this class, either directly or indirectly.
-
-
Constructor Summary
Constructors Constructor Description Specification()
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
noExceptionThrown()
Specifies that no exception should be thrown, failing with aUnallowedExceptionThrownError
otherwise.void
notThrown(Class<? extends Throwable> type)
Specifies that no exception of the given type should be thrown, failing with aUnallowedExceptionThrownError
otherwise.<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>
Tthrown()
Specifies that the preceding when block should throw an exception.<T extends Throwable>
Tthrown(Class<T> type)
Specifies that the preceding when block should throw an exception of the given type.void
verifyAll(Closure closure)
All assertions in this block are executed and the errors recorded and reported at the end.<U> void
verifyAll(Object target, Class<U> type, Closure closure)
A combination ofwith(Object, Class, Closure)
andverifyAll(Closure)
.<U> void
verifyAll(U target, Closure<?> closure)
A combination ofwith(Object, Closure)
andverifyAll(Closure)
.<U> void
with(Object target, Class<U> type, Closure closure)
Same aswith(Object, groovy.lang.Closure)
, except that it also states that the specified target has the specified type, throwing aSpockAssertionError
otherwise.<U> void
with(U target, Closure<?> closure)
Sets the specified object as the implicit target of the top-level conditions and/or interactions contained in the specified code block, thereby avoiding the need to repeat the same expression multiple times.-
Methods inherited from class spock.mock.MockingApi
GroovyMock, GroovyMock, GroovyMock, GroovyMock, GroovyMock, GroovyMock, GroovyMock, GroovyMock, GroovySpy, GroovySpy, GroovySpy, GroovySpy, GroovySpy, GroovySpy, GroovySpy, GroovySpy, GroovyStub, GroovyStub, GroovyStub, GroovyStub, GroovyStub, GroovyStub, GroovyStub, GroovyStub, interaction, Mock, Mock, Mock, Mock, Mock, Mock, Mock, Mock, Spy, Spy, Spy, Spy, Spy, Spy, Spy, Spy, Spy, Spy, Stub, Stub, Stub, Stub, Stub, Stub, Stub, Stub
-
-
-
-
Field Detail
-
_
public static final Object _
The wildcard symbol. Used in several places as a don't care value:- Mock interactions Example: 1 * foo.bar(_)
- Data parameterizations Example: [foo, _] << loadDataFromDb()
-
-
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 no exception of the given type should be thrown, failing with aUnallowedExceptionThrownError
otherwise.- Parameters:
type
- the exception type that should not be thrown
-
noExceptionThrown
public void noExceptionThrown()
Specifies that no exception should be thrown, failing with aUnallowedExceptionThrownError
otherwise.
-
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
-
with
@Beta public <U> void with(U target, @DelegatesTo(strategy=1) Closure<?> closure)
Sets the specified object as the implicit target of the top-level conditions and/or interactions contained in the specified code block, thereby avoiding the need to repeat the same expression multiple times. Implicit conditions are supported. (In other words, theassert
keyword may be omitted.) If the target isnull
, aSpockAssertionError
is thrown.A
with
block can be used anywhere in a spec, including nested positions and helper methods.Condition example:
def fred = new Person(name: "Fred", age: 42) def spaceship = new Spaceship(pilot: fred) expect: with(spaceship.pilot) { name == "Fred" // shorthand for: spaceship.pilot.name == "Fred" age == 42 }
Interaction example:
def service = Mock(Service) // has start(), stop(), and doWork() methods def app = new Application(service) // controls the lifecycle of the service when: app.run() then: with(service) { 1 * start() // shorthand for: 1 * service.start() 1 * doWork() 1 * stop() }
- Type Parameters:
U
- type of target- Parameters:
target
- an implicit target for conditions and/or interactionsclosure
- a code block containing top-level conditions and/or interactions
-
with
@Beta public <U> void with(Object target, Class<U> type, @DelegatesTo(genericTypeIndex=0,strategy=1) Closure closure)
Same aswith(Object, groovy.lang.Closure)
, except that it also states that the specified target has the specified type, throwing aSpockAssertionError
otherwise. As a side effect, this may give better code completion in IDEs.Example:
def fred = new Employee(name: "Fred", age: 42, employer: "MarsTravelUnited") def spaceship = new Spaceship(pilot: fred) expect: with(spaceship.pilot, Employee) { name == "Fred" // shorthand for: spaceship.pilot.name == "Fred" age == 42 employer == "MarsTravelUnited" }
- Type Parameters:
U
- type of target- Parameters:
target
- an implicit target for conditions and/or interactionstype
- the expected type of the targetclosure
- a code block containing top-level conditions and/or interactions
-
verifyAll
@Beta public void verifyAll(Closure closure)
All assertions in this block are executed and the errors recorded and reported at the end.Example:
expect: verifyAll { 1 == 2 2 == 3 }
This will report two errors, instead of just the first.- Parameters:
closure
- a code block containing top-level conditions and/or interactions
-
verifyAll
@Beta public <U> void verifyAll(U target, @DelegatesTo(strategy=1) Closure<?> closure)
A combination ofwith(Object, Closure)
andverifyAll(Closure)
.- Type Parameters:
U
- type of target- Parameters:
target
- an implicit target for conditions and/or interactionsclosure
- a code block containing top-level conditions and/or interactions- Since:
- 1.2
-
verifyAll
@Beta public <U> void verifyAll(Object target, Class<U> type, @DelegatesTo(genericTypeIndex=0,strategy=1) Closure closure)
A combination ofwith(Object, Class, Closure)
andverifyAll(Closure)
.- Type Parameters:
U
- type of target- Parameters:
target
- an implicit target for conditions and/or interactionstype
- the expected type of the targetclosure
- a code block containing top-level conditions and/or interactions- Since:
- 1.2
-
-