Specificationpublic class MockingApi extends SpecInternals
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 | Description |
|---|---|
MockingApi() |
| Modifier and Type | Method | Description |
|---|---|---|
java.lang.Object |
GroovyMock() |
Creates a Groovy mock whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
java.lang.Object |
GroovyMock(groovy.lang.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(java.lang.Class<T> type) |
Creates a Groovy mock with the specified type.
|
<T> T |
GroovyMock(java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a Groovy mock with the specified type and interactions.
|
java.lang.Object |
GroovyMock(java.util.Map<java.lang.String,java.lang.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.
|
java.lang.Object |
GroovyMock(java.util.Map<java.lang.String,java.lang.Object> options,
groovy.lang.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 |
GroovyMock(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type) |
Creates a Groovy mock with the specified options and type.
|
<T> T |
GroovyMock(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a Groovy mock with the specified options, type, and interactions.
|
java.lang.Object |
GroovySpy() |
Creates a Groovy spy whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
java.lang.Object |
GroovySpy(groovy.lang.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(java.lang.Class<T> type) |
Creates a Groovy spy with the specified type.
|
<T> T |
GroovySpy(java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a Groovy spy with the specified type and interactions.
|
java.lang.Object |
GroovySpy(java.util.Map<java.lang.String,java.lang.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.
|
java.lang.Object |
GroovySpy(java.util.Map<java.lang.String,java.lang.Object> options,
groovy.lang.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(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type) |
Creates a Groovy spy with the specified options and type.
|
<T> T |
GroovySpy(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a Groovy spy with the specified options, type, and interactions.
|
java.lang.Object |
GroovyStub() |
Creates a Groovy stub whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
java.lang.Object |
GroovyStub(groovy.lang.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(java.lang.Class<T> type) |
Creates a Groovy stub with the specified type.
|
<T> T |
GroovyStub(java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a Groovy stub with the specified type and interactions.
|
java.lang.Object |
GroovyStub(java.util.Map<java.lang.String,java.lang.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.
|
java.lang.Object |
GroovyStub(java.util.Map<java.lang.String,java.lang.Object> options,
groovy.lang.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.
|
<T> T |
GroovyStub(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type) |
Creates a Groovy stub with the specified options and type.
|
<T> T |
GroovyStub(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a Groovy stub with the specified options, type, and interactions.
|
void |
interaction(groovy.lang.Closure block) |
Encloses one or more interaction definitions in a then block.
|
java.lang.Object |
Mock() |
Creates a mock whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
java.lang.Object |
Mock(groovy.lang.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(java.lang.Class<T> type) |
Creates a mock with the specified type.
|
<T> T |
Mock(java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a mock with the specified type and interactions.
|
java.lang.Object |
Mock(java.util.Map<java.lang.String,java.lang.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.
|
java.lang.Object |
Mock(java.util.Map<java.lang.String,java.lang.Object> options,
groovy.lang.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.
|
<T> T |
Mock(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type) |
Creates a mock with the specified options and type.
|
<T> T |
Mock(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a mock with the specified options, type, and interactions.
|
java.lang.Object |
Spy() |
Creates a spy whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
java.lang.Object |
Spy(groovy.lang.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(java.lang.Class<T> type) |
Creates a spy with the specified type.
|
<T> T |
Spy(java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a spy with the specified type and interactions.
|
java.lang.Object |
Spy(java.util.Map<java.lang.String,java.lang.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.
|
java.lang.Object |
Spy(java.util.Map<java.lang.String,java.lang.Object> options,
groovy.lang.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(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type) |
Creates a spy with the specified options and type.
|
<T> T |
Spy(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a spy with the specified options, type, and interactions.
|
java.lang.Object |
Stub() |
Creates a stub whose type and name are inferred from the left-hand side of the enclosing variable assignment.
|
java.lang.Object |
Stub(groovy.lang.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(java.lang.Class<T> type) |
Creates a stub with the specified type.
|
<T> T |
Stub(java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a stub with the specified type and interactions.
|
java.lang.Object |
Stub(java.util.Map<java.lang.String,java.lang.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.
|
java.lang.Object |
Stub(java.util.Map<java.lang.String,java.lang.Object> options,
groovy.lang.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.
|
<T> T |
Stub(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type) |
Creates a stub with the specified options and type.
|
<T> T |
Stub(java.util.Map<java.lang.String,java.lang.Object> options,
java.lang.Class<T> type,
groovy.lang.Closure interactions) |
Creates a stub with the specified options, type, and interactions.
|
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, waitcreateMock, getSpecificationContextpublic void interaction(groovy.lang.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 java.lang.Object Mock()
Person person = Mock() // type is Person.class, name is "person"
@Beta public java.lang.Object Mock(java.util.Map<java.lang.String,java.lang.Object> options)
Person person = Mock(name: "myPerson") // type is Person.class, name is "myPerson"
options - optional options for creating the mockpublic <T> T Mock(java.lang.Class<T> type)
def person = Mock(Person) // type is Person.class, name is "person"
T - the interface or class type of the mocktype - the interface or class type of the mock@Beta public <T> T Mock(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type)
def person = Mock(Person, name: "myPerson") // type is Person.class, name is "myPerson"
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 java.lang.Object Mock(groovy.lang.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 java.lang.Object Mock(java.util.Map<java.lang.String,java.lang.Object> options, groovy.lang.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(java.lang.Class<T> type, groovy.lang.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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type, groovy.lang.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 java.lang.Object Stub()
Person person = Stub() // type is Person.class, name is "person"
@Beta public java.lang.Object Stub(java.util.Map<java.lang.String,java.lang.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(java.lang.Class<T> type)
def person = Stub(Person) // type is Person.class, name is "person"
T - the interface or class type of the stubtype - the interface or class type of the stub@Beta public <T> T Stub(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type)
def person = Stub(Person, name: "myPerson") // type is Person.class, name is "myPerson"
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 java.lang.Object Stub(groovy.lang.Closure interactions)
// type is Person.class, name is "person", returns hard-coded values for propertynameand methodsing()Person person = Stub { name << "Fred" sing() << "Tra-la-la" }
interactions - a description of the stub's interactions@Beta public java.lang.Object Stub(java.util.Map<java.lang.String,java.lang.Object> options, groovy.lang.Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded values for propertynameand 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(java.lang.Class<T> type, groovy.lang.Closure interactions)
// name is "person", type is Person.class, returns hard-coded values for propertynameand 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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type, groovy.lang.Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded values for propertynameand 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 java.lang.Object Spy()
Person person = Spy() // type is Person.class, name is "person"
@Beta public java.lang.Object Spy(java.util.Map<java.lang.String,java.lang.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(java.lang.Class<T> type)
def person = Spy(Person) // type is Person.class, name is "person"
T - the class type of the spytype - the class type of the spy@Beta public <T> T Spy(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type)
def person = Spy(Person, name: "myPerson") // type is Person.class, name is "myPerson"
T - the class type of the spyoptions - optional options for creating the spytype - the class type of the spy@Beta public java.lang.Object Spy(groovy.lang.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 java.lang.Object Spy(java.util.Map<java.lang.String,java.lang.Object> options, groovy.lang.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(java.lang.Class<T> type, groovy.lang.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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type, groovy.lang.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 java.lang.Object GroovyMock()
Person person = GroovyMock() // type is Person.class, name is "person"
@Beta public java.lang.Object GroovyMock(java.util.Map<java.lang.String,java.lang.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(java.lang.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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.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 java.lang.Object GroovyMock(groovy.lang.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 java.lang.Object GroovyMock(java.util.Map<java.lang.String,java.lang.Object> options, groovy.lang.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(java.lang.Class<T> type, groovy.lang.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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type, groovy.lang.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 java.lang.Object GroovyStub()
Person person = GroovyStub() // type is Person.class, name is "person"
@Beta public java.lang.Object GroovyStub(java.util.Map<java.lang.String,java.lang.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(java.lang.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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.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 java.lang.Object GroovyStub(groovy.lang.Closure interactions)
// type is Person.class, name is "person", returns hard-coded values for propertynameand methodsing()Person person = GroovyStub { name << "Fred" sing() << "Tra-la-la" }
interactions - a description of the Groovy stub's interactions@Beta public java.lang.Object GroovyStub(java.util.Map<java.lang.String,java.lang.Object> options, groovy.lang.Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded values for propertynameand 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(java.lang.Class<T> type, groovy.lang.Closure interactions)
// name is "person", type is Person.class, returns hard-coded values for propertynameand 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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type, groovy.lang.Closure interactions)
// type is Person.class, name is "myPerson", returns hard-coded values for propertynameand 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 java.lang.Object GroovySpy()
Person person = GroovySpy() // type is Person.class, name is "person"
@Beta public java.lang.Object GroovySpy(java.util.Map<java.lang.String,java.lang.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(java.lang.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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.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 java.lang.Object GroovySpy(groovy.lang.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 java.lang.Object GroovySpy(java.util.Map<java.lang.String,java.lang.Object> options, groovy.lang.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(java.lang.Class<T> type, groovy.lang.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(java.util.Map<java.lang.String,java.lang.Object> options, java.lang.Class<T> type, groovy.lang.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 interactionsCopyright © 2018. All rights reserved