public abstract class Specification extends MockingApi
| Constructor and Description |
|---|
Specification() |
| Modifier and Type | Method and Description |
|---|---|
void |
noExceptionThrown()
Specifies that no exception should be thrown, failing with a
UnallowedExceptionThrownError otherwise. |
void |
notThrown(Class<? extends Throwable> type)
Specifies that no exception of the given type should be
thrown, failing with a
UnallowedExceptionThrownError 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> |
thrown()
Specifies that the preceding when block should throw an exception.
|
<T extends Throwable> |
thrown(Class<T> type)
Specifies that the preceding when block should throw an exception
of the given type.
|
void |
verifyAll(Closure closure) |
void |
with(Object target,
Class<?> type,
Closure closure)
Same as
with(Object, groovy.lang.Closure), except that it also states that
the specified target has the specified type, throwing a SpockAssertionError
otherwise. |
void |
with(Object 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.
|
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, Stub, Stub, Stub, Stub, Stub, Stub, Stub, Stubpublic static final Object _
public <T extends Throwable> T thrown()
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(...)
public <T extends Throwable> T thrown(Class<T> type)
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)
T - the expected exception typetype - the expected exception typepublic void notThrown(Class<? extends Throwable> type)
UnallowedExceptionThrownError otherwise.type - the exception type that should not be thrownpublic void noExceptionThrown()
UnallowedExceptionThrownError otherwise.public <T> T old(T expression)
T - the expression's typeexpression - an arbitrary expression, except that it may not
reference variables defined in the then-block@Beta public void with(Object target, Closure<?> closure)
assert keyword may be omitted.) If the target is null, a
SpockAssertionError 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()
}
target - an implicit target for conditions and/or interactionsclosure - a code block containing top-level conditions and/or interactions@Beta public void with(Object target, Class<?> type, Closure closure)
with(Object, groovy.lang.Closure), except that it also states that
the specified target has the specified type, throwing a SpockAssertionError
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"
}
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@Beta public void verifyAll(Closure closure)