Package spock.mock

Interface MockFactory

All Known Implementing Classes:
DetachedMockFactory, MockingApi, Specification

public interface MockFactory
Base interface for Java based mocks see MockingApi or DetachedMockFactory for more examples.
  • Method Summary

    Modifier and Type Method Description
    <T> T Mock​(Class<T> type)
    Creates a mock with the specified type.
    <T> T Mock​(Map<String,​Object> options, Class<T> type)
    Creates a mock with the specified options and type.
    <T> T Spy​(Class<T> type)
    Creates a spy with the specified type.
    <T> T Spy​(Map<String,​Object> options, Class<T> type)
    Creates a spy with the specified options and type.
    <T> T Spy​(T obj)
    Creates a spy wrapping a provided instance.
    <T> T Stub​(Class<T> type)
    Creates a stub with the specified type.
    <T> T Stub​(Map<String,​Object> options, Class<T> type)
    Creates a stub with the specified options and type.
  • Method Details

    • Mock

      <T> T Mock​(Class<T> type)
      Creates a mock with the specified type. Example:
         def person = Mock(Person) // type is Person.class, name is "person"
       
      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

      @Beta <T> T Mock​(Map<String,​Object> options, Class<T> type)
      Creates a mock with the specified options and type. Example:
         def person = Mock(Person, name: "myPerson") // type is Person.class, name is "myPerson"
       
      Type Parameters:
      T - the interface or class type of the mock
      Parameters:
      options - optional options for creating the mock
      type - the interface or class type of the mock
      Returns:
      a mock with the specified options and type
    • Stub

      @Beta <T> T Stub​(Class<T> type)
      Creates a stub with the specified type. Example:
         def person = Stub(Person) // type is Person.class, name is "person"
       
      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

      @Beta <T> T Stub​(Map<String,​Object> options, Class<T> type)
      Creates a stub with the specified options and type. Example:
         def person = Stub(Person, name: "myPerson") // type is Person.class, name is "myPerson"
       
      Type Parameters:
      T - the interface or class type of the stub
      Parameters:
      options - optional options for creating the stub
      type - the interface or class type of the stub
      Returns:
      a stub with the specified options and type
    • Spy

      @Beta <T> T Spy​(Class<T> type)
      Creates a spy with the specified type. Example:
         def person = Spy(Person) // type is Person.class, name is "person"
       
      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

      @Beta <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"
       
      Type Parameters:
      T - the class type of the spy
      Parameters:
      obj - the instance to spy
      Returns:
      a spy with the specified type
    • Spy

      @Beta <T> T Spy​(Map<String,​Object> options, Class<T> type)
      Creates a spy with the specified options and type. Example:
         def person = Spy(Person, name: "myPerson") // type is Person.class, name is "myPerson"
       
      Type Parameters:
      T - the class type of the spy
      Parameters:
      options - optional options for creating the spy
      type - the class type of the spy
      Returns:
      a spy with the specified options and type