Package spock.mock

Class DetachedMockFactory

  • All Implemented Interfaces:
    MockFactory

    @Beta
    public class DetachedMockFactory
    extends java.lang.Object
    implements MockFactory
    This factory allows the creations of mocks outside of a Specification, e.g., in a Spring configuration.

    In order to be usable those Mocks must be manually attached to the Specification using MockUtil.attachMock(Object, Specification) and detached afterwards MockUtil.detachMock(Object).

    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      <T> T createMock​(java.lang.String name, java.lang.Class<T> type, org.spockframework.mock.MockNature nature, java.util.Map<java.lang.String,​java.lang.Object> options)  
      <T> T createMock​(java.lang.String name, T obj, org.spockframework.mock.MockNature nature, java.util.Map<java.lang.String,​java.lang.Object> options)  
      <T> T Mock​(java.lang.Class<T> type)
      Creates a mock with the specified type.
      <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 Spy​(java.lang.Class<T> type)
      Creates a spy with the specified type.
      <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​(T obj)
      Creates a spy wrapping a provided instance.
      <T> T Stub​(java.lang.Class<T> type)
      Creates a stub with the specified type.
      <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.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Constructor Detail

      • DetachedMockFactory

        public DetachedMockFactory()
    • Method Detail

      • Mock

        public <T> T Mock​(java.lang.Class<T> type)
        Creates a mock with the specified type. The mock name will be the types simple name. Example:
           def person = Mock(Person) // type is Person.class, name is "person"
         
        Specified by:
        Mock in interface MockFactory
        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

        public <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. The mock name will be the types simple name. Example:
           def person = Mock(Person, name: "myPerson") // type is Person.class, name is "myPerson"
         
        Specified by:
        Mock in interface MockFactory
        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

        public <T> T Stub​(java.lang.Class<T> type)
        Creates a stub with the specified type. The mock name will be the types simple name. Example:
           def person = Stub(Person) // type is Person.class, name is "person"
         
        Specified by:
        Stub in interface MockFactory
        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

        public <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. The mock name will be the types simple name. Example:
           def person = Stub(Person, name: "myPerson") // type is Person.class, name is "myPerson"
         
        Specified by:
        Stub in interface MockFactory
        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

        public <T> T Spy​(java.lang.Class<T> type)
        Creates a spy with the specified type. The mock name will be the types simple name. Example:
           def person = Spy(Person) // type is Person.class, name is "person"
         
        Specified by:
        Spy in interface MockFactory
        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)
        Description copied from interface: MockFactory
        Creates a spy wrapping a provided instance. Example:
           def person = Spy(new Person()) // type is Person.class, name is "person"
         
        Specified by:
        Spy in interface MockFactory
        Type Parameters:
        T - the class type of the spy
        Parameters:
        obj - the instance to spy
        Returns:
        a spy with the specified type
      • Spy

        public <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. The mock name will be the types simple name. Example:
           def person = Spy(Person, name: "myPerson") // type is Person.class, name is "myPerson"
         
        Specified by:
        Spy in interface MockFactory
        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
      • createMock

        public <T> T createMock​(java.lang.String name,
                                java.lang.Class<T> type,
                                org.spockframework.mock.MockNature nature,
                                java.util.Map<java.lang.String,​java.lang.Object> options)
      • createMock

        public <T> T createMock​(java.lang.String name,
                                T obj,
                                org.spockframework.mock.MockNature nature,
                                java.util.Map<java.lang.String,​java.lang.Object> options)