Package spock.mock
Class MockingApi
java.lang.Object
spock.mock.MockingApi
- All Implemented Interfaces:
MockFactory
- Direct Known Subclasses:
Specification
Spock's mocking API primarily consists of the following factory methods:
- Mock()
- Creates a general-purpose test double that supports both stubbing and mocking.
- Stub()
- Creates a test double that supports stubbing but not mocking.
- Spy()
- Creates a test double that, by default, delegates all calls to a real object. Supports both stubbing and mocking.
- GroovyMock()
- Creates a Mock() with additional, Groovy-specific features.
- GroovyStub()
- Creates a Stub() with additional, Groovy-specific features.
- GroovySpy()
- Creates a Spy() with additional, Groovy-specific features.
- SpyStatic()
- Mocks static methods of the given type that, by default, delegates all calls to the real static methods. Supports both stubbing and mocking.
- type
- The interface or class type of the mock. If not present and the mock is created as part of a variable assignment, the type is inferred from the variable's type (if possible).
- options
- Additional options for creating the mock. Typically passed as named arguments.
See
IMockConfigurationfor available options. - block
- A code block that allows to specify interactions right when creating the mock.
def mock = Mock(Person)
Person mock = Mock()
def spy = Spy(Person, constructorArgs: ["Fred"])
Person stub = Stub {
getName() >> "Fred"
sing() >> "Tra-la-la"
}
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescription<T> TCreates a Groovy mock whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TGroovyMock(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> TGroovyMock(Class<T> type) Creates a Groovy mock with the specified type.<T> TGroovyMock(Class<T> type, Closure interactions) Creates a Groovy mock with the specified type and interactions.<T> TGroovyMock(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> TGroovyMock(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> TGroovyMock(Map<String, Object> options, Class<T> type) Creates a Groovy mock with the specified options and type.<T> TCreates a Groovy mock with the specified options, type, and interactions.<T> TCreates a Groovy spy whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TCreates a Groovy spy with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment.<T> TCreates a Groovy spy with the specified type.<T> TCreates a Groovy spy with the specified type and interactions.<T> TCreates a Groovy spy with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TCreates 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> TCreates a Groovy spy with the specified options and type.<T> TCreates a Groovy spy with the specified options, type, and interactions.<T> TGroovySpy(T obj) Creates a Groovy spy wrapping a provided instance.<T> TCreates a Groovy spy with the specified interactions wrapping a provided instance.<T> TCreates a Groovy stub whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TGroovyStub(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> TGroovyStub(Class<T> type) Creates a Groovy stub with the specified type.<T> TGroovyStub(Class<T> type, Closure interactions) Creates a Groovy stub with the specified type and interactions.<T> TGroovyStub(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> TGroovyStub(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.<T> TGroovyStub(Map<String, Object> options, Class<T> type) Creates a Groovy stub with the specified options and type.<T> TCreates a Groovy stub with the specified options, type, and interactions.voidinteraction(Closure block) Encloses one or more interaction definitions in a then block.<T> TMock()Creates a mock whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TCreates a mock with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment.<T> TCreates a mock with the specified type.<T> TCreates a mock with the specified type and interactions.<T> TCreates a mock with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TCreates a mock with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment.<T> TCreates a mock with the specified options and type.<T> TCreates a mock with the specified options, type, and interactions.voidRuns the code with the thread-aware mocks activated on the currentThread.<T> TSpy()Creates a spy whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TCreates a spy with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment.<T> TCreates a spy with the specified type.<T> TCreates a spy with the specified type and interactions.<T> TCreates a spy with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TCreates a spy with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment.<T> TCreates a spy with the specified options and type.<T> TCreates a spy with the specified options, type, and interactions.<T> TSpy(T obj) Creates a spy wrapping a provided instance.<T> TCreates a spy with the specified interactions wrapping a provided instance.<T> voidCreates a thread-local spy for all static methods of the passed type.<T> voidSpyStatic(Class<T> type, IMockMakerSettings mockMakerSettings) Creates a thread-local spy for all static methods of the passed type.<T> TStub()Creates a stub whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TCreates a stub with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment.<T> TCreates a stub with the specified type.<T> TCreates a stub with the specified type and interactions.<T> TCreates a stub with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment.<T> TCreates a stub with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment.<T> TCreates a stub with the specified options and type.<T> TCreates a stub with the specified options, type, and interactions.<R> RwithActiveThreadAwareMocks(Callable<R> code) Runs the code with the thread-aware mocks activated on the currentThread.
-
Constructor Details
-
MockingApi
public MockingApi()
-
-
Method Details
-
interaction
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:- num * target.method(args)
- target.method(args) >>(>) result(s)
- num * target.method(args) >>(>) result(s)
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
-
Mock
public <T> T Mock()Creates a mock whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = Mock() // type is Person.class, name is "person"
- Returns:
- a mock whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
Mock
Creates a mock with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = Mock(name: "myPerson") // type is Person.class, name is "myPerson"
- Parameters:
options- optional options for creating the mock- Returns:
- a mock with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
Mock
Creates a mock with the specified type. If enclosed in a variable assignment, the variable name will be used as the mock's name. Example:def person = Mock(Person) // type is Person.class, name is "person"
- Specified by:
Mockin interfaceMockFactory- Type Parameters:
T- the interface or class type of the mock- Parameters:
type- the interface or class type of the mock- Returns:
- a mock with the specified type
-
Mock
Creates a mock with the specified options and type. If enclosed in an variable assignment, the variable name will be used as the mock's name. Example:def person = Mock(Person, name: "myPerson") // type is Person.class, name is "myPerson"
- Specified by:
Mockin interfaceMockFactory- Type Parameters:
T- the interface or class type of the mock- Parameters:
options- optional options for creating the mocktype- the interface or class type of the mock- Returns:
- a mock with the specified options and type
-
Mock
public <T> T Mock(@ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) Closure interactions) Creates a mock with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment. Example:// type is Person.class, name is "person", returns hard-coded value for
name, expects one call tosing()Person person = Mock { name >> "Fred" 1 * sing() }- Parameters:
interactions- a description of the mock's interactions- Returns:
- a mock with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
Mock
public <T> T Mock(Map<String, Object> options, @ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "myPerson", returns hard-coded value for
name, expects one call tosing()Person person = Mock(name: "myPerson") { name >> "Fred" 1 * sing() }- Parameters:
options- optional options for creating the mockinteractions- a description of the mock's interactions- Returns:
- a mock with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
Mock
public <T> T Mock(@Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.FirstParam.FirstGenericType.class) Closure interactions) Creates a mock with the specified type and interactions. If enclosed in a variable assignment, the variable name will be used as the mock's name. Example:// name is "person", type is Person.class, returns hard-code value
name, expects one call tosing()def person = Mock(Person) { name >> "Fred" 1 * sing() }- Type Parameters:
T- the interface or class type of the mock- Parameters:
type- the interface or class type of the mockinteractions- a description of the mock's interactions- Returns:
- a mock with the specified type and interactions
-
Mock
public <T> T Mock(Map<String, Object> options, @Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.SecondParam.FirstGenericType.class) Closure interactions) Creates a mock with the specified options, type, and interactions. If enclosed in a variable assignment, the variable name will be used as the mock's name. Example:// type is Person.class, name is "myPerson", returns hard-coded value
name, expects one call tosing()def person = Mock(Person, name: "myPerson") { name >> "Fred" 1 * sing() }- Type Parameters:
T- the interface or class type of the mock- Parameters:
options- options for creating the mock (seeIMockConfigurationfor available options})type- the interface or class type of the mockinteractions- a description of the mock's interactions- Returns:
- a mock with the specified options, type, and interactions
-
Stub
public <T> T Stub()Creates a stub whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = Stub() // type is Person.class, name is "person"
- Returns:
- a stub whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
Stub
Creates a stub with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = Stub(name: "myPerson") // type is Person.class, name is "myPerson"
- Parameters:
options- optional options for creating the stub- Returns:
- a stub with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
Stub
Creates a stub with the specified type. If enclosed in a variable assignment, the variable name will be used as the stub's name. Example:def person = Stub(Person) // type is Person.class, name is "person"
- Specified by:
Stubin interfaceMockFactory- Type Parameters:
T- the interface or class type of the stub- Parameters:
type- the interface or class type of the stub- Returns:
- a stub with the specified type
-
Stub
Creates a stub with the specified options and type. If enclosed in an variable assignment, the variable name will be used as the stub's name. Example:def person = Stub(Person, name: "myPerson") // type is Person.class, name is "myPerson"
- Specified by:
Stubin interfaceMockFactory- Type Parameters:
T- the interface or class type of the stub- Parameters:
options- optional options for creating the stubtype- the interface or class type of the stub- Returns:
- a stub with the specified options and type
-
Stub
public <T> T Stub(@ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) Closure interactions) Creates a stub with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment. Example:// type is Person.class, name is "person", returns hard-coded values for property
nameand methodsing()Person person = Stub { name >> "Fred" sing() >> "Tra-la-la" }- Parameters:
interactions- a description of the stub's interactions- Returns:
- a stub with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
Stub
public <T> T Stub(Map<String, Object> options, @ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "myPerson", returns hard-coded values for property
nameand methodsing()Person person = Stub(name: "myPerson") { name >> "Fred" sing() >> "Tra-la-la" }- Parameters:
options- optional options for creating the stubinteractions- a description of the stub's interactions- Returns:
- a stub with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
Stub
public <T> T Stub(@Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.FirstParam.FirstGenericType.class) Closure interactions) Creates a stub with the specified type and interactions. If enclosed in a variable assignment, the variable name will be used as the stub's name. Example:// name is "person", type is Person.class, returns hard-coded values for property
nameand methodsing()def person = Stub(Person) { name >> "Fred" sing() >> "Tra-la-la" }- Type Parameters:
T- the interface or class type of the stub- Parameters:
type- the interface or class type of the stubinteractions- a description of the stub's interactions- Returns:
- a stub with the specified type and interactions
-
Stub
public <T> T Stub(Map<String, Object> options, @Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.SecondParam.FirstGenericType.class) Closure interactions) Creates a stub with the specified options, type, and interactions. If enclosed in a variable assignment, the variable name will be used as the stub's name. Example:// type is Person.class, name is "myPerson", returns hard-coded values for property
nameand methodsing()def person = Stub(Person, name: "myPerson") { name >> "Fred" sing() >> "Tra-la-la" }- Type Parameters:
T- the interface or class type of the stub- Parameters:
options- options for creating the stub (seeIMockConfigurationfor available options})type- the interface or class type of the stubinteractions- a description of the stub's interactions- Returns:
- a stub with the specified options, type, and interactions
-
Spy
public <T> T Spy()Creates a spy whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = Spy() // type is Person.class, name is "person"
- Returns:
- a spy whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
Spy
Creates a spy with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = Spy(name: "myPerson") // type is Person.class, name is "myPerson"
- Parameters:
options- optional options for creating the spy- Returns:
- a spy with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
Spy
Creates a spy with the specified type. If enclosed in a variable assignment, the variable name will be used as the spy's name. Example:def person = Spy(Person) // type is Person.class, name is "person"
- Specified by:
Spyin interfaceMockFactory- Type Parameters:
T- the class type of the spy- Parameters:
type- the class type of the spy- Returns:
- a spy with the specified type
-
Spy
public <T> T Spy(T obj) Creates a spy wrapping a provided instance. Example: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.- Specified by:
Spyin interfaceMockFactory- Type Parameters:
T- the class type of the spy- Parameters:
obj- the instance to spy- Returns:
- a spy wrapping the provided instance
- Since:
- 1.1
-
Spy
public <T> T Spy(@Target T obj, @DelegatesTo(strategy=1) @ClosureParams(groovy.transform.stc.FirstParam.class) Closure interactions) Creates a spy with the specified interactions wrapping a provided instance. Example: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.- Type Parameters:
T- the class type of the spy- Parameters:
obj- the instance to spyinteractions- a description of the spy's interactions- Returns:
- a spy with the specified interactions wrapping the provided instance
- Since:
- 2.0
-
Spy
Creates a spy with the specified options and type. If enclosed in an variable assignment, the variable name will be used as the spy's name. Example:def person = Spy(Person, name: "myPerson") // type is Person.class, name is "myPerson"
- Specified by:
Spyin interfaceMockFactory- Type Parameters:
T- the class type of the spy- Parameters:
options- optional options for creating the spytype- the class type of the spy- Returns:
- a spy with the specified options and type
-
Spy
public <T> T Spy(@ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) Closure interactions) Creates a spy with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment. Example:// type is Person.class, name is "person", returns hard-coded value for
name, calls real method otherwise Person person = Spy { name >> "Fred" }- Parameters:
interactions- a description of the spy's interactions- Returns:
- a spy with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
Spy
public <T> T Spy(Map<String, Object> options, @ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "myPerson", returns hard-coded value for
name, calls real method otherwise Person person = Spy(name: "myPerson") { name >> "Fred" }- Parameters:
options- optional options for creating the spyinteractions- a description of the spy's interactions- Returns:
- a spy with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
Spy
public <T> T Spy(@Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.FirstParam.FirstGenericType.class) Closure interactions) Creates a spy with the specified type and interactions. If enclosed in a variable assignment, the variable name will be used as the spy's name. Example:// name is "person", type is Person.class, returns hard-code value
name, calls real method otherwise def person = Spy(Person) { name >> "Fred" 1 * sing() }- Type Parameters:
T- the class type of the spy- Parameters:
type- the class type of the spyinteractions- a description of the spy's interactions- Returns:
- a spy with the specified type and interactions
-
Spy
public <T> T Spy(Map<String, Object> options, @Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.SecondParam.FirstGenericType.class) Closure interactions) Creates a spy with the specified options, type, and interactions. If enclosed in a variable assignment, the variable name will be used as the spy's name. Example:// type is Person.class, name is "myPerson", returns hard-coded value
name, calls real method otherwise def person = Spy(Person, name: "myPerson") { name >> "Fred" }- Type Parameters:
T- the class type of the spy- Parameters:
options- options for creating the spy (seeIMockConfigurationfor available options})type- the class type of the spyinteractions- a description of the spy's interactions- Returns:
- a spy with the specified options, type, and interactions
-
GroovyMock
public <T> T GroovyMock()Creates a Groovy mock whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = GroovyMock() // type is Person.class, name is "person"
- Returns:
- a Groovy mock whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
GroovyMock
Creates a Groovy mock with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = GroovyMock(name: "myPerson") // type is Person.class, name is "myPerson"
- Parameters:
options- optional options for creating the Groovy mock- Returns:
- a Groovy mock with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
GroovyMock
Creates a Groovy mock with the specified type. If enclosed in a variable assignment, the variable name will be used as the mock's name. Example:def person = GroovyMock(Person) // type is Person.class, name is "person"
- Type Parameters:
T- the interface or class type of the Groovy mock- Parameters:
type- the interface or class type of the Groovy mock- Returns:
- a Groovy mock with the specified type
-
GroovyMock
Creates a Groovy mock with the specified options and type. If enclosed in an variable assignment, the variable name will be used as the mock's name. Example:def person = GroovyMock(Person, name: "myPerson") // type is Person.class, name is "myPerson"
- Type Parameters:
T- the interface or class type of the Groovy mock- Parameters:
options- optional options for creating the Groovy mocktype- the interface or class type of the Groovy mock- Returns:
- a Groovy mock with the specified options and type
-
GroovyMock
public <T> T GroovyMock(@ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "person", returns hard-coded value for
name, expects one call tosing()Person person = GroovyMock { name >> "Fred" 1 * sing() }- Parameters:
interactions- a description of the Groovy mock's interactions- Returns:
- a Groovy mock with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
GroovyMock
public <T> T GroovyMock(Map<String, Object> options, @ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "myPerson", returns hard-coded value for
name, expects one call tosing()Person person = GroovyMock(name: "myPerson") { name >> "Fred" 1 * sing() }- Parameters:
options- optional options for creating the Groovy mockinteractions- a description of the Groovy mock's interactions- Returns:
- a Groovy mock with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
GroovyMock
public <T> T GroovyMock(@Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.FirstParam.FirstGenericType.class) Closure interactions) Creates a Groovy mock with the specified type and interactions. If enclosed in a variable assignment, the variable name will be used as the mock's name. Example:// name is "person", type is Person.class, returns hard-code value
name, expects one call tosing()def person = GroovyMock(Person) { name >> "Fred" 1 * sing() }- Type Parameters:
T- the interface or class type of the Groovy mock- Parameters:
type- the interface or class type of the Groovy mockinteractions- a description of the Groovy mock's interactions- Returns:
- a Groovy mock with the specified type and interactions
-
GroovyMock
public <T> T GroovyMock(Map<String, Object> options, @Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.SecondParam.FirstGenericType.class) Closure interactions) Creates a Groovy mock with the specified options, type, and interactions. If enclosed in a variable assignment, the variable name will be used as the mock's name. Example:// type is Person.class, name is "myPerson", returns hard-coded value
name, expects one call tosing()def person = GroovyMock(Person, name: "myPerson") { name >> "Fred" 1 * sing() }- Type Parameters:
T- the interface or class type of the Groovy mock- Parameters:
options- options for creating the Groovy mock (seeIMockConfigurationfor available options})type- the interface or class type of the mockinteractions- a description of the Groovy mock's interactions- Returns:
- a Groovy mock with the specified options, type, and interactions
-
GroovyStub
public <T> T GroovyStub()Creates a Groovy stub whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = GroovyStub() // type is Person.class, name is "person"
- Returns:
- a Groovy stub whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
GroovyStub
Creates a Groovy stub with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = GroovyStub(name: "myPerson") // type is Person.class, name is "myPerson"
- Parameters:
options- optional options for creating the Groovy stub- Returns:
- a Groovy stub with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
GroovyStub
Creates a Groovy stub with the specified type. If enclosed in a variable assignment, the variable name will be used as the stub's name. Example:def person = GroovyStub(Person) // type is Person.class, name is "person"
- Type Parameters:
T- the interface or class type of the Groovy stub- Parameters:
type- the interface or class type of the Groovy stub- Returns:
- a Groovy stub with the specified type
-
GroovyStub
Creates a Groovy stub with the specified options and type. If enclosed in an variable assignment, the variable name will be used as the stub's name. Example:def person = GroovyStub(Person, name: "myPerson") // type is Person.class, name is "myPerson"
- Type Parameters:
T- the interface or class type of the Groovy stub- Parameters:
options- optional options for creating the Groovy stubtype- the interface or class type of the Groovy stub- Returns:
- a Groovy stub with the specified options and type
-
GroovyStub
public <T> T GroovyStub(@ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "person", returns hard-coded values for property
nameand methodsing()Person person = GroovyStub { name >> "Fred" sing() >> "Tra-la-la" }- Parameters:
interactions- a description of the Groovy stub's interactions- Returns:
- a Groovy stub with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
GroovyStub
public <T> T GroovyStub(Map<String, Object> options, @ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "myPerson", returns hard-coded values for property
nameand methodsing()Person person = GroovyStub(name: "myPerson") { name >> "Fred" sing() >> "Tra-la-la" }- Parameters:
options- optional options for creating the Groovy stubinteractions- a description of the Groovy stub's interactions- Returns:
- a Groovy stub with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
GroovyStub
public <T> T GroovyStub(@Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.FirstParam.FirstGenericType.class) Closure interactions) Creates a Groovy stub with the specified type and interactions. If enclosed in a variable assignment, the variable name will be used as the stub's name. Example:// name is "person", type is Person.class, returns hard-coded values for property
nameand methodsing()def person = GroovyStub(Person) { name >> "Fred" sing() >> "Tra-la-la" }- Type Parameters:
T- the interface or class type of the Groovy stub- Parameters:
type- the interface or class type of the Groovy stubinteractions- a description of the Groovy stub's interactions- Returns:
- a Groovy stub with the specified type and interactions
-
GroovyStub
public <T> T GroovyStub(Map<String, Object> options, @Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.SecondParam.FirstGenericType.class) Closure interactions) Creates a Groovy stub with the specified options, type, and interactions. If enclosed in a variable assignment, the variable name will be used as the stub's name. Example:// type is Person.class, name is "myPerson", returns hard-coded values for property
nameand methodsing()def person = GroovyStub(Person, name: "myPerson") { name >> "Fred" sing() >> "Tra-la-la" }- Type Parameters:
T- the interface or class type of the Groovy stub- Parameters:
options- options for creating the Groovy stub (seeIMockConfigurationfor available options})type- the interface or class type of the Groovy stubinteractions- a description of the Groovy stub's interactions- Returns:
- a Groovy stub with the specified options, type, and interactions
-
GroovySpy
public <T> T GroovySpy()Creates a Groovy spy whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = GroovySpy() // type is Person.class, name is "person"
- Returns:
- a Groovy spy whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
GroovySpy
Creates a Groovy spy with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment. Example:Person person = GroovySpy(name: "myPerson") // type is Person.class, name is "myPerson"
- Parameters:
options- optional options for creating the Groovy spy- Returns:
- a Groovy spy with the specified options whose type and name are inferred from the left-hand side of the enclosing variable assignment
-
GroovySpy
Creates a Groovy spy with the specified type. If enclosed in a variable assignment, the variable name will be used as the spy's name. Example:def person = GroovySpy(Person) // type is Person.class, name is "person"
- Type Parameters:
T- the class type of the Groovy spy- Parameters:
type- the class type of the Groovy spy- Returns:
- a Groovy spy with the specified type
-
GroovySpy
@Beta public <T> T GroovySpy(T obj) Creates a Groovy spy wrapping a provided instance. Example: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.- Type Parameters:
T- the class type of the spy- Parameters:
obj- the instance to spy- Returns:
- a spy wrapping the provided instance
- Since:
- 2.4
-
GroovySpy
@Beta public <T> T GroovySpy(@Target T obj, @DelegatesTo(strategy=1) @ClosureParams(groovy.transform.stc.FirstParam.class) Closure interactions) Creates a Groovy spy with the specified interactions wrapping a provided instance. Example: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.- Type Parameters:
T- the class type of the spy- Parameters:
obj- the instance to spyinteractions- a description of the spy's interactions- Returns:
- a spy with the specified interactions wrapping the provided instance
- Since:
- 2.4
-
GroovySpy
Creates a Groovy spy with the specified options and type. If enclosed in an variable assignment, the variable name will be used as the spy's name. Example:def person = GroovySpy(Person, name: "myPerson") // type is Person.class, name is "myPerson"
- Type Parameters:
T- the class type of the Groovy spy- Parameters:
options- optional options for creating the Groovy spytype- the class type of the Groovy spy- Returns:
- a Groovy spy with the specified options and type
-
GroovySpy
public <T> T GroovySpy(@ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "person", returns hard-coded value for
name, calls real method otherwise Person person = GroovySpy { name >> "Fred" }- Parameters:
interactions- a description of the spy's interactions- Returns:
- a Groovy spy with the specified interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
GroovySpy
public <T> T GroovySpy(Map<String, Object> options, @ClosureParams(org.spockframework.mock.ClosureParameterTypeFromVariableType.class) 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. Example:// type is Person.class, name is "myPerson", returns hard-coded value for
name, calls real method otherwise Person person = GroovySpy(name: "myPerson") { name >> "Fred" }- Parameters:
options- optional options for creating the Groovy spyinteractions- a description of the Groovy spy's interactions- Returns:
- a Groovy spy with the specified options and interactions whose type and name are inferred from the left-hand side of the enclosing assignment
-
GroovySpy
public <T> T GroovySpy(@Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.FirstParam.FirstGenericType.class) Closure interactions) Creates a Groovy spy with the specified type and interactions. If enclosed in a variable assignment, the variable name will be used as the spy's name. Example:// name is "person", type is Person.class, returns hard-code value
name, calls real method otherwise def person = GroovySpy(Person) { name >> "Fred" 1 * sing() }- Type Parameters:
T- the class type of the Groovy spy- Parameters:
type- the class type of the Groovy spyinteractions- a description of the Groovy spy's interactions- Returns:
- a Groovy spy with the specified type and interactions
-
GroovySpy
public <T> T GroovySpy(Map<String, Object> options, @Target Class<T> type, @DelegatesTo(strategy=1,genericTypeIndex=0) @ClosureParams(groovy.transform.stc.SecondParam.FirstGenericType.class) Closure interactions) Creates a Groovy spy with the specified options, type, and interactions. If enclosed in a variable assignment, the variable name will be used as the spy's name. Example:// type is Person.class, name is "myPerson", returns hard-coded value
name, calls real method otherwise def person = GroovySpy(Person, name: "myPerson") { name >> "Fred" }- Type Parameters:
T- the class type of the Groovy spy- Parameters:
options- options for creating the Groovy spy (seeIMockConfigurationfor available options)type- the class type of the Groovy spyinteractions- a description of the Groovy spy's interactions- Returns:
- a Groovy spy with the specified options, type, and interactions
-
SpyStatic
Creates a thread-local spy for all static methods of the passed type.Example:
SpyStatic(Person)
If you want to activate the static mocks on a different
Thread, please callrunWithThreadAwareMocks(Runnable)on the differentThread.- Parameters:
type- the type of which the static methods shall be spied
-
SpyStatic
Creates a thread-local spy for all static methods of the passed type.Example:
SpyStatic(Person, spock.mock.MockMakers.mockitoInline)
If you want to activate the static mocks on a different
Thread, please callrunWithThreadAwareMocks(Runnable)on the differentThread.- Parameters:
type- the type of which the static methods shall be spiedmockMakerSettings- the mock maker settings to apply to the static spy
-
runWithThreadAwareMocks
Runs the code with the thread-aware mocks activated on the currentThread.Note: You only need this if your current
Threadis not the test thread. On the testThread, the thread-aware mocks are automatically activated.- Parameters:
code- the code to execute
-
withActiveThreadAwareMocks
Runs the code with the thread-aware mocks activated on the currentThread.Note: You only need this if your current
Threadis not the test thread. On the testThread, the thread-aware mocks are automatically activated.- Type Parameters:
R- the return type- Parameters:
code- the code to execute- Returns:
- the return value of the executed code
-