public class MockingApi extends org.spockframework.lang.SpecInternals implements MockFactory
IMockConfiguration
for available options.def mock = Mock(Person) Person mock = Mock() def spy = Spy(Person, constructorArgs: ["Fred"]) Person stub = Stub { getName() >> "Fred" sing() >> "Tra-la-la" }
Constructor and Description |
---|
MockingApi() |
Modifier and Type | Method and Description |
---|---|
<T> T |
GroovyMock()
Creates a Groovy mock whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
<T> T |
GroovyMock(Class<T> type)
Creates a Groovy mock with the specified type.
|
<T> T |
GroovyMock(Class<T> type,
Closure interactions)
Creates a Groovy mock with the specified type and interactions.
|
<T> T |
GroovyMock(Closure interactions)
Creates a Groovy mock with the specified interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
GroovyMock(Map<String,Object> options)
Creates a Groovy mock with the specified options whose type and name are inferred from the left-hand side of the
enclosing variable assignment.
|
<T> T |
GroovyMock(Map<String,Object> options,
Class<T> type)
Creates a Groovy mock with the specified options and type.
|
<T> T |
GroovyMock(Map<String,Object> options,
Class<T> type,
Closure interactions)
Creates a Groovy mock with the specified options, type, and interactions.
|
<T> T |
GroovyMock(Map<String,Object> options,
Closure interactions)
Creates a Groovy mock with the specified options and interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
GroovySpy()
Creates a Groovy spy whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
<T> T |
GroovySpy(Class<T> type)
Creates a Groovy spy with the specified type.
|
<T> T |
GroovySpy(Class<T> type,
Closure interactions)
Creates a Groovy spy with the specified type and interactions.
|
<T> T |
GroovySpy(Closure interactions)
Creates a Groovy spy with the specified interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
GroovySpy(Map<String,Object> options)
Creates a Groovy spy with the specified options whose type and name are inferred from the left-hand side of the
enclosing variable assignment.
|
<T> T |
GroovySpy(Map<String,Object> options,
Class<T> type)
Creates a Groovy spy with the specified options and type.
|
<T> T |
GroovySpy(Map<String,Object> options,
Class<T> type,
Closure interactions)
Creates a Groovy spy with the specified options, type, and interactions.
|
<T> T |
GroovySpy(Map<String,Object> options,
Closure interactions)
Creates a Groovy spy with the specified options and interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
GroovySpy(T obj)
Creates a Groovy spy wrapping a provided instance.
|
<T> T |
GroovySpy(T obj,
Closure interactions)
Creates a Groovy spy with the specified interactions wrapping a provided instance.
|
<T> T |
GroovyStub()
Creates a Groovy stub whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
<T> T |
GroovyStub(Class<T> type)
Creates a Groovy stub with the specified type.
|
<T> T |
GroovyStub(Class<T> type,
Closure interactions)
Creates a Groovy stub with the specified type and interactions.
|
<T> T |
GroovyStub(Closure interactions)
Creates a Groovy stub with the specified interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
GroovyStub(Map<String,Object> options)
Creates a Groovy stub with the specified options whose type and name are inferred from the left-hand side of the
enclosing variable assignment.
|
<T> T |
GroovyStub(Map<String,Object> options,
Class<T> type)
Creates a Groovy stub with the specified options and type.
|
<T> T |
GroovyStub(Map<String,Object> options,
Class<T> type,
Closure interactions)
Creates a Groovy stub with the specified options, type, and interactions.
|
<T> T |
GroovyStub(Map<String,Object> options,
Closure interactions)
Creates a Groovy stub with the specified options and interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
void |
interaction(Closure block)
Encloses one or more interaction definitions in a then block.
|
<T> T |
Mock()
Creates a mock whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
<T> T |
Mock(Class<T> type)
Creates a mock with the specified type.
|
<T> T |
Mock(Class<T> type,
Closure interactions)
Creates a mock with the specified type and interactions.
|
<T> T |
Mock(Closure interactions)
Creates a mock with the specified interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
Mock(Map<String,Object> options)
Creates a mock with the specified options whose type and name are inferred from the left-hand side of the
enclosing variable assignment.
|
<T> T |
Mock(Map<String,Object> options,
Class<T> type)
Creates a mock with the specified options and type.
|
<T> T |
Mock(Map<String,Object> options,
Class<T> type,
Closure interactions)
Creates a mock with the specified options, type, and interactions.
|
<T> T |
Mock(Map<String,Object> options,
Closure interactions)
Creates a mock with the specified options and interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
void |
runWithThreadAwareMocks(Runnable code)
Runs the code with the thread-aware mocks activated on the current
Thread . |
<T> T |
Spy()
Creates a spy whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
<T> T |
Spy(Class<T> type)
Creates a spy with the specified type.
|
<T> T |
Spy(Class<T> type,
Closure interactions)
Creates a spy with the specified type and interactions.
|
<T> T |
Spy(Closure interactions)
Creates a spy with the specified interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
Spy(Map<String,Object> options)
Creates a spy with the specified options whose type and name are inferred from the left-hand side of the
enclosing variable assignment.
|
<T> T |
Spy(Map<String,Object> options,
Class<T> type)
Creates a spy with the specified options and type.
|
<T> T |
Spy(Map<String,Object> options,
Class<T> type,
Closure interactions)
Creates a spy with the specified options, type, and interactions.
|
<T> T |
Spy(Map<String,Object> options,
Closure interactions)
Creates a spy with the specified options and interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
Spy(T obj)
Creates a spy wrapping a provided instance.
|
<T> T |
Spy(T obj,
Closure interactions)
Creates a spy with the specified interactions wrapping a provided instance.
|
<T> void |
SpyStatic(Class<T> type)
Creates a thread-local spy for all static methods of the passed type.
|
<T> void |
SpyStatic(Class<T> type,
IMockMakerSettings mockMakerSettings)
Creates a thread-local spy for all static methods of the passed type.
|
<T> T |
Stub()
Creates a stub whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
<T> T |
Stub(Class<T> type)
Creates a stub with the specified type.
|
<T> T |
Stub(Class<T> type,
Closure interactions)
Creates a stub with the specified type and interactions.
|
<T> T |
Stub(Closure interactions)
Creates a stub with the specified interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<T> T |
Stub(Map<String,Object> options)
Creates a stub with the specified options whose type and name are inferred from the left-hand side of the
enclosing variable assignment.
|
<T> T |
Stub(Map<String,Object> options,
Class<T> type)
Creates a stub with the specified options and type.
|
<T> T |
Stub(Map<String,Object> options,
Class<T> type,
Closure interactions)
Creates a stub with the specified options, type, and interactions.
|
<T> T |
Stub(Map<String,Object> options,
Closure interactions)
Creates a stub with the specified options and interactions whose type and name are inferred
from the left-hand side of the enclosing assignment.
|
<R> R |
withActiveThreadAwareMocks(Callable<R> code)
Runs the code with the thread-aware mocks activated on the current
Thread . |
public void interaction(Closure block)
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) }
block
- a block of code containing one or more interaction definitionspublic <T> T Mock()
Person person = Mock() // type is Person.class, name is "person"
@Beta public <T> T Mock(Map<String,Object> options)
Person person = Mock(name: "myPerson") // type is Person.class, name is "myPerson"
options
- optional options for creating the mockpublic <T> T Mock(Class<T> type)
def person = Mock(Person) // type is Person.class, name is "person"
Mock
in interface MockFactory
T
- the interface or class type of the mocktype
- the interface or class type of the mock@Beta public <T> T Mock(Map<String,Object> options, Class<T> type)
def person = Mock(Person, name: "myPerson") // type is Person.class, name is "myPerson"
Mock
in interface MockFactory
T
- the interface or class type of the mockoptions
- optional options for creating the mocktype
- the interface or class type of the mock@Beta public <T> T Mock(Closure interactions)
// type is Person.class, name is "person", returns hard-coded value forname
, expects one call tosing()
Person person = Mock { name >> "Fred" 1 * sing() }
interactions
- a description of the mock's interactions@Beta public <T> T Mock(Map<String,Object> options, Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded value forname
, expects one call tosing()
Person person = Mock(name: "myPerson") { name >> "Fred" 1 * sing() }
options
- optional options for creating the mockinteractions
- a description of the mock's interactions@Beta public <T> T Mock(Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// name is "person", type is Person.class, returns hard-code valuename
, expects one call tosing()
def person = Mock(Person) { name >> "Fred" 1 * sing() }
T
- the interface or class type of the mocktype
- the interface or class type of the mockinteractions
- a description of the mock's interactions@Beta public <T> T Mock(Map<String,Object> options, Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded valuename
, expects one call tosing()
def person = Mock(Person, name: "myPerson") { name >> "Fred" 1 * sing() }
T
- the interface or class type of the mockoptions
- options for creating the mock (see IMockConfiguration
for available options})type
- the interface or class type of the mockinteractions
- a description of the mock's interactions@Beta public <T> T Stub()
Person person = Stub() // type is Person.class, name is "person"
@Beta public <T> T Stub(Map<String,Object> options)
Person person = Stub(name: "myPerson") // type is Person.class, name is "myPerson"
options
- optional options for creating the stub@Beta public <T> T Stub(Class<T> type)
def person = Stub(Person) // type is Person.class, name is "person"
Stub
in interface MockFactory
T
- the interface or class type of the stubtype
- the interface or class type of the stub@Beta public <T> T Stub(Map<String,Object> options, Class<T> type)
def person = Stub(Person, name: "myPerson") // type is Person.class, name is "myPerson"
Stub
in interface MockFactory
T
- the interface or class type of the stuboptions
- optional options for creating the stubtype
- the interface or class type of the stub@Beta public <T> T Stub(Closure interactions)
// type is Person.class, name is "person", returns hard-coded values for propertyname
and methodsing()
Person person = Stub { name >> "Fred" sing() >> "Tra-la-la" }
interactions
- a description of the stub's interactions@Beta public <T> T Stub(Map<String,Object> options, Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded values for propertyname
and methodsing()
Person person = Stub(name: "myPerson") { name >> "Fred" sing() >> "Tra-la-la" }
options
- optional options for creating the stubinteractions
- a description of the stub's interactions@Beta public <T> T Stub(Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// name is "person", type is Person.class, returns hard-coded values for propertyname
and methodsing()
def person = Stub(Person) { name >> "Fred" sing() >> "Tra-la-la" }
T
- the interface or class type of the stubtype
- the interface or class type of the stubinteractions
- a description of the stub's interactions@Beta public <T> T Stub(Map<String,Object> options, Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded values for propertyname
and methodsing()
def person = Stub(Person, name: "myPerson") { name >> "Fred" sing() >> "Tra-la-la" }
T
- the interface or class type of the stuboptions
- options for creating the stub (see IMockConfiguration
for available options})type
- the interface or class type of the stubinteractions
- a description of the stub's interactions@Beta public <T> T Spy()
Person person = Spy() // type is Person.class, name is "person"
@Beta public <T> T Spy(Map<String,Object> options)
Person person = Spy(name: "myPerson") // type is Person.class, name is "myPerson"
options
- optional options for creating the spy@Beta public <T> T Spy(Class<T> type)
def person = Spy(Person) // type is Person.class, name is "person"
Spy
in interface MockFactory
T
- the class type of the spytype
- the class type of the spy@Beta public <T> T Spy(T obj)
def person = Spy(new Person()) // type is Person.class, name is "person"You need to use the spy returned by this method instead of the original instance, otherwise interactions won't be picked up.
Spy
in interface MockFactory
T
- the class type of the spyobj
- the instance to spy@Beta public <T> T Spy(T obj, @DelegatesTo(strategy=1) Closure interactions)
def person = Spy(new Person()) { name >> "Fred" }You need to use the spy returned by this method instead of the original instance, otherwise interactions won't be picked up.
T
- the class type of the spyobj
- the instance to spyinteractions
- a description of the spy's interactions@Beta public <T> T Spy(Map<String,Object> options, Class<T> type)
def person = Spy(Person, name: "myPerson") // type is Person.class, name is "myPerson"
Spy
in interface MockFactory
T
- the class type of the spyoptions
- optional options for creating the spytype
- the class type of the spy@Beta public <T> T Spy(Closure interactions)
// type is Person.class, name is "person", returns hard-coded value for name
, calls real method otherwise
Person person = Spy {
name >> "Fred"
}
interactions
- a description of the spy's interactions@Beta public <T> T Spy(Map<String,Object> options, Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded value for name
, calls real method otherwise
Person person = Spy(name: "myPerson") {
name >> "Fred"
}
options
- optional options for creating the spyinteractions
- a description of the spy's interactions@Beta public <T> T Spy(Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// name is "person", type is Person.class, returns hard-code value name
, calls real method otherwise
def person = Spy(Person) {
name >> "Fred"
1 * sing()
}
T
- the class type of the spytype
- the class type of the spyinteractions
- a description of the spy's interactions@Beta public <T> T Spy(Map<String,Object> options, Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded value name
, calls real method otherwise
def person = Spy(Person, name: "myPerson") {
name >> "Fred"
}
T
- the class type of the spyoptions
- options for creating the spy (see IMockConfiguration
for available options})type
- the class type of the spyinteractions
- a description of the spy's interactions@Beta public <T> T GroovyMock()
Person person = GroovyMock() // type is Person.class, name is "person"
@Beta public <T> T GroovyMock(Map<String,Object> options)
Person person = GroovyMock(name: "myPerson") // type is Person.class, name is "myPerson"
options
- optional options for creating the Groovy mock@Beta public <T> T GroovyMock(Class<T> type)
def person = GroovyMock(Person) // type is Person.class, name is "person"
T
- the interface or class type of the Groovy mocktype
- the interface or class type of the Groovy mock@Beta public <T> T GroovyMock(Map<String,Object> options, Class<T> type)
def person = GroovyMock(Person, name: "myPerson") // type is Person.class, name is "myPerson"
T
- the interface or class type of the Groovy mockoptions
- optional options for creating the Groovy mocktype
- the interface or class type of the Groovy mock@Beta public <T> T GroovyMock(Closure interactions)
// type is Person.class, name is "person", returns hard-coded value forname
, expects one call tosing()
Person person = GroovyMock { name >> "Fred" 1 * sing() }
interactions
- a description of the Groovy mock's interactions@Beta public <T> T GroovyMock(Map<String,Object> options, Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded value forname
, expects one call tosing()
Person person = GroovyMock(name: "myPerson") { name >> "Fred" 1 * sing() }
options
- optional options for creating the Groovy mockinteractions
- a description of the Groovy mock's interactions@Beta public <T> T GroovyMock(Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// name is "person", type is Person.class, returns hard-code valuename
, expects one call tosing()
def person = GroovyMock(Person) { name >> "Fred" 1 * sing() }
T
- the interface or class type of the Groovy mocktype
- the interface or class type of the Groovy mockinteractions
- a description of the Groovy mock's interactions@Beta public <T> T GroovyMock(Map<String,Object> options, Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded valuename
, expects one call tosing()
def person = GroovyMock(Person, name: "myPerson") { name >> "Fred" 1 * sing() }
T
- the interface or class type of the Groovy mockoptions
- options for creating the Groovy mock (see IMockConfiguration
for available options})type
- the interface or class type of the mockinteractions
- a description of the Groovy mock's interactions@Beta public <T> T GroovyStub()
Person person = GroovyStub() // type is Person.class, name is "person"
@Beta public <T> T GroovyStub(Map<String,Object> options)
Person person = GroovyStub(name: "myPerson") // type is Person.class, name is "myPerson"
options
- optional options for creating the Groovy stub@Beta public <T> T GroovyStub(Class<T> type)
def person = GroovyStub(Person) // type is Person.class, name is "person"
T
- the interface or class type of the Groovy stubtype
- the interface or class type of the Groovy stub@Beta public <T> T GroovyStub(Map<String,Object> options, Class<T> type)
def person = GroovyStub(Person, name: "myPerson") // type is Person.class, name is "myPerson"
T
- the interface or class type of the Groovy stuboptions
- optional options for creating the Groovy stubtype
- the interface or class type of the Groovy stub@Beta public <T> T GroovyStub(Closure interactions)
// type is Person.class, name is "person", returns hard-coded values for propertyname
and methodsing()
Person person = GroovyStub { name >> "Fred" sing() >> "Tra-la-la" }
interactions
- a description of the Groovy stub's interactions@Beta public <T> T GroovyStub(Map<String,Object> options, Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded values for propertyname
and methodsing()
Person person = GroovyStub(name: "myPerson") { name >> "Fred" sing() >> "Tra-la-la" }
options
- optional options for creating the Groovy stubinteractions
- a description of the Groovy stub's interactions@Beta public <T> T GroovyStub(Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// name is "person", type is Person.class, returns hard-coded values for propertyname
and methodsing()
def person = GroovyStub(Person) { name >> "Fred" sing() >> "Tra-la-la" }
T
- the interface or class type of the Groovy stubtype
- the interface or class type of the Groovy stubinteractions
- a description of the Groovy stub's interactions@Beta public <T> T GroovyStub(Map<String,Object> options, Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded values for propertyname
and methodsing()
def person = GroovyStub(Person, name: "myPerson") { name >> "Fred" sing() >> "Tra-la-la" }
T
- the interface or class type of the Groovy stuboptions
- options for creating the Groovy stub (see IMockConfiguration
for available options})type
- the interface or class type of the Groovy stubinteractions
- a description of the Groovy stub's interactions@Beta public <T> T GroovySpy()
Person person = GroovySpy() // type is Person.class, name is "person"
@Beta public <T> T GroovySpy(Map<String,Object> options)
Person person = GroovySpy(name: "myPerson") // type is Person.class, name is "myPerson"
options
- optional options for creating the Groovy spy@Beta public <T> T GroovySpy(Class<T> type)
def person = GroovySpy(Person) // type is Person.class, name is "person"
T
- the class type of the Groovy spytype
- the class type of the Groovy spy@Beta public <T> T GroovySpy(T obj)
def person = GroovySpy(new Person()) // type is Person.class, name is "person"You need to use the spy returned by this method instead of the original instance, otherwise interactions won't be picked up.
T
- the class type of the spyobj
- the instance to spy@Beta public <T> T GroovySpy(T obj, @DelegatesTo(strategy=1) Closure interactions)
def person = GroovySpy(new Person()) { name >> "Fred" }You need to use the spy returned by this method instead of the original instance, otherwise interactions won't be picked up.
T
- the class type of the spyobj
- the instance to spyinteractions
- a description of the spy's interactions@Beta public <T> T GroovySpy(Map<String,Object> options, Class<T> type)
def person = GroovySpy(Person, name: "myPerson") // type is Person.class, name is "myPerson"
T
- the class type of the Groovy spyoptions
- optional options for creating the Groovy spytype
- the class type of the Groovy spy@Beta public <T> T GroovySpy(Closure interactions)
// type is Person.class, name is "person", returns hard-coded value for name
, calls real method otherwise
Person person = GroovySpy {
name >> "Fred"
}
interactions
- a description of the spy's interactions@Beta public <T> T GroovySpy(Map<String,Object> options, Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded value for name
, calls real method otherwise
Person person = GroovySpy(name: "myPerson") {
name >> "Fred"
}
options
- optional options for creating the Groovy spyinteractions
- a description of the Groovy spy's interactions@Beta public <T> T GroovySpy(Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// name is "person", type is Person.class, returns hard-code value name
, calls real method otherwise
def person = GroovySpy(Person) {
name >> "Fred"
1 * sing()
}
T
- the class type of the Groovy spytype
- the class type of the Groovy spyinteractions
- a description of the Groovy spy's interactions@Beta public <T> T GroovySpy(Map<String,Object> options, Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded value name
, calls real method otherwise
def person = GroovySpy(Person, name: "myPerson") {
name >> "Fred"
}
T
- the class type of the Groovy spyoptions
- options for creating the Groovy spy (see IMockConfiguration
for available options)type
- the class type of the Groovy spyinteractions
- a description of the Groovy spy's interactions@Beta public <T> void SpyStatic(Class<T> type)
Example:
SpyStatic(Person)
If you want to activate the static mocks on a different Thread
,
please call runWithThreadAwareMocks(Runnable)
on the different Thread
.
type
- the type of which the static methods shall be spied@Beta public <T> void SpyStatic(Class<T> type, IMockMakerSettings mockMakerSettings)
Example:
SpyStatic(Person, spock.mock.MockMakers.mockitoInline)
If you want to activate the static mocks on a different Thread
,
please call runWithThreadAwareMocks(Runnable)
on the different Thread
.
type
- the type of which the static methods shall be spiedmockMakerSettings
- the mock maker settings to apply to the static spypublic void runWithThreadAwareMocks(Runnable code)
Thread
.
Note: You only need this if your current Thread
is not the test thread.
On the test Thread
, the thread-aware mocks are automatically activated.
code
- the code to executepublic <R> R withActiveThreadAwareMocks(Callable<R> code)
Thread
.
Note: You only need this if your current Thread
is not the test thread.
On the test Thread
, the thread-aware mocks are automatically activated.
R
- the return typecode
- the code to execute