Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## org.mockito.internal.creation
Class MockSettingsImpl<T>

    java.lang.Object
      org.mockito.internal.creation.settings.CreationSettings<T>
          org.mockito.internal.creation.MockSettingsImpl<T>

All Implemented Interfaces:
  java.io.Serializable, MockCreationSettings<T>, MockSettings

    public class MockSettingsImpl<T>

extends CreationSettings<T>
implements MockSettings, MockCreationSettings<T>

See Also:
  Serialized Form

Field Summary

Fields inherited from class org.mockito.internal.creation.settings.CreationSettings
`defaultAnswer, extraInterfaces, invocationListeners, mockName, name, serializable, spiedInstance, typeToMock`

Constructor Summary
`MockSettingsImpl()`

Method Summary
`MockCreationSettings<T>` | `confirm(java.lang.Class<T> typeToMock)`
`MockSettings` | `defaultAnswer(Answer defaultAnswer)` Specifies default answers to interactions.
`MockSettings` | `extraInterfaces(java.lang.Class... extraInterfaces)` Specifies extra interfaces the mock should implement.
`Answer<java.lang.Object>` | `getDefaultAnswer()` the default answer for this mock, see `MockSettings.defaultAnswer(org.mockito.stubbing.Answer)`.
`java.util.Set<java.lang.Class>` | `getExtraInterfaces()` the extra interfaces the mock object should implement.
`java.util.List<InvocationListener>` | `getInvocationListeners()` the invocation listeners attached to this mock, see `MockSettings.invocationListeners(org.mockito.listeners.InvocationListener...)`.
`MockName` | `getMockName()` the name of this mock, as printed on verification errors; see `MockSettings.name(java.lang.String)`.
`java.lang.Object` | `getSpiedInstance()` the spied instance - needed for spies.
`java.lang.Class<T>` | `getTypeToMock()` Mocked type.
`boolean` | `hasInvocationListeners()`
`MockSettings` | `invocationListeners(InvocationListener... listeners)` Registers a listener for method invocations on this mock.
`boolean` | `isSerializable()` if the mock is serializable, see `MockSettings.serializable()`.
`MockSettings` | `name(java.lang.String name)` Specifies mock name.
`MockSettings` | `serializable()` Configures the mock to be serializable.
`MockSettings` | `spiedInstance(java.lang.Object spiedInstance)` Specifies the instance to spy on.
`MockSettings` | `verboseLogging()` Enables real-time logging of method invocations on this mock.

Methods inherited from class org.mockito.internal.creation.settings.CreationSettings
`getName, setExtraInterfaces, setMockName, setTypeToMock`

Methods inherited from class java.lang.Object
`clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Constructor Detail

### MockSettingsImpl

    public MockSettingsImpl()

Method Detail

### serializable

    public MockSettings serializable()

  Description copied from interface: `MockSettings`
  Configures the mock to be serializable. With this feature you can use a mock in a place that requires dependencies to be serializable.

  WARNING: This should be rarely used in unit testing.

  The behaviour was implemented for a specific use case of a BDD spec that had an unreliable external dependency. This was in a web environment and the objects from the external dependency were being serialized to pass between layers.

  Example:

         List serializableMock = mock(List.class, withSettings().serializable());

  Specified by:
    `serializable` in interface `MockSettings`
  Returns:
    settings instance so that you can fluently specify other settings

### extraInterfaces

    public MockSettings extraInterfaces(java.lang.Class... extraInterfaces)

  Description copied from interface: `MockSettings`
  Specifies extra interfaces the mock should implement. Might be useful for legacy code or some corner cases. For background, see issue 51 here

  This mysterious feature should be used very occasionally. The object under test should know exactly its collaborators & dependencies. If you happen to use it often than please make sure you are really producing simple, clean & readable code.

  Examples:

         Foo foo = mock(Foo.class, withSettings().extraInterfaces(Bar.class, Baz.class));

         //now, the mock implements extra interfaces, so following casting is possible:
         Bar bar = (Bar) foo;
         Baz baz = (Baz) foo;

  Specified by:
    `extraInterfaces` in interface `MockSettings`
  Parameters:
    `extraInterfaces` - extra interfaces the should implement.
  Returns:
    settings instance so that you can fluently specify other settings

### getMockName

    public MockName getMockName()

  Description copied from interface: `MockCreationSettings`
  the name of this mock, as printed on verification errors; see `MockSettings.name(java.lang.String)`.

  Specified by:
    `getMockName` in interface `MockCreationSettings<T>`
  Overrides:
    `getMockName` in class `CreationSettings<T>`

### getExtraInterfaces

    public java.util.Set<java.lang.Class> getExtraInterfaces()

  Description copied from interface: `MockCreationSettings`
  the extra interfaces the mock object should implement.

  Specified by:
    `getExtraInterfaces` in interface `MockCreationSettings<T>`
  Overrides:
    `getExtraInterfaces` in class `CreationSettings<T>`

### getSpiedInstance

    public java.lang.Object getSpiedInstance()

  Description copied from interface: `MockCreationSettings`
  the spied instance - needed for spies.

  Specified by:
    `getSpiedInstance` in interface `MockCreationSettings<T>`
  Overrides:
    `getSpiedInstance` in class `CreationSettings<T>`

### name

    public MockSettings name(java.lang.String name)

  Description copied from interface: `MockSettings`
  Specifies mock name. Naming mocks can be helpful for debugging - the name is used in all verification errors.

  Beware that naming mocks is not a solution for complex code which uses too many mocks or collaborators. If you have too many mocks then refactor the code so that it's easy to test/debug without necessity of naming mocks.

  If you use @Mock annotation then you've got naming mocks for free! @Mock uses field name as mock name. `Read more.`

  Examples:

         Foo foo = mock(Foo.class, withSettings().name("foo"));

         //Below does exactly the same:
         Foo foo = mock(Foo.class, "foo");

  Specified by:
    `name` in interface `MockSettings`
  Parameters:
    `name` - the name of the mock, later used in all verification errors
  Returns:
    settings instance so that you can fluently specify other settings

### spiedInstance

    public MockSettings spiedInstance(java.lang.Object spiedInstance)

  Description copied from interface: `MockSettings`
  Specifies the instance to spy on. Makes sense only for spies/partial mocks. Sets the instance that will be spied. Actually copies the internal fields of the passed instance to the mock.

  As usual you are going to read the partial mock warning: Object oriented programming is more or less about tackling complexity by dividing the complexity into separate, specific, SRPy objects. How does partial mock fit into this paradigm? Well, it just doesn't... Partial mock usually means that the complexity has been moved to a different method on the same object. In most cases, this is not the way you want to design your application.

  However, there are rare cases when partial mocks come handy: dealing with code you cannot change easily (3rd party interfaces, interim refactoring of legacy code etc.) However, I wouldn't use partial mocks for new, test-driven & well-designed code.

  Enough warnings about partial mocks, see an example how spiedInstance() works:

         Foo foo = mock(Foo.class, withSettings().spiedInstance(fooInstance));

         //Below does exactly the same:
         Foo foo = spy(fooInstance);

  About stubbing for a partial mock, as it is a spy it will always call the real method, unless you use the `doReturn`|`Throw`|`Answer`|`CallRealMethod` stubbing style. Example:

         List list = new LinkedList();
         List spy = spy(list);

         //Impossible: real method is called so spy.get(0) throws IndexOutOfBoundsException (the list is yet empty)
         when(spy.get(0)).thenReturn("foo");

         //You have to use doReturn() for stubbing
         doReturn("foo").when(spy).get(0);

  Specified by:
    `spiedInstance` in interface `MockSettings`
  Parameters:
    `spiedInstance` - to spy on
  Returns:
    settings instance so that you can fluently specify other settings

### defaultAnswer

    public MockSettings defaultAnswer(Answer defaultAnswer)

  Description copied from interface: `MockSettings`
  Specifies default answers to interactions. It's quite advanced feature and typically you don't need it to write decent tests. However it can be helpful when working with legacy systems.

  It is the default answer so it will be used only when you don't stub the method call.

         Foo mock = mock(Foo.class, withSettings().defaultAnswer(RETURNS_SMART_NULLS));
         Foo mockTwo = mock(Foo.class, withSettings().defaultAnswer(new YourOwnAnswer()));

         //Below does exactly the same:
         Foo mockTwo = mock(Foo.class, new YourOwnAnswer());

  Specified by:
    `defaultAnswer` in interface `MockSettings`
  Parameters:
    `defaultAnswer` - default answer to be used by mock when not stubbed
  Returns:
    settings instance so that you can fluently specify other settings

### getDefaultAnswer

    public Answer<java.lang.Object> getDefaultAnswer()

  Description copied from interface: `MockCreationSettings`
  the default answer for this mock, see `MockSettings.defaultAnswer(org.mockito.stubbing.Answer)`.

  Specified by:
    `getDefaultAnswer` in interface `MockCreationSettings<T>`
  Overrides:
    `getDefaultAnswer` in class `CreationSettings<T>`

### isSerializable

    public boolean isSerializable()

  Description copied from interface: `MockCreationSettings`
  if the mock is serializable, see `MockSettings.serializable()`.

  Specified by:
    `isSerializable` in interface `MockCreationSettings<T>`
  Overrides:
    `isSerializable` in class `CreationSettings<T>`

### verboseLogging

    public MockSettings verboseLogging()

  Description copied from interface: `MockSettings`
  Enables real-time logging of method invocations on this mock. Can be used during test debugging in order to find wrong interactions with this mock.

  Invocations are logged as they happen to the standard output stream.

  Calling this method multiple times makes no difference.

  Example:

       List mockWithLogger = mock(List.class, withSettings().verboseLogging());

  Specified by:
    `verboseLogging` in interface `MockSettings`
  Returns:
    settings instance so that you can fluently specify other settings

### invocationListeners

    public MockSettings invocationListeners(InvocationListener... listeners)

  Description copied from interface: `MockSettings`
  Registers a listener for method invocations on this mock. The listener is notified every time a method on this mock is called.

  Multiple listeners may be added, but the same object is only added once. The order, in which the listeners are added, is not guaranteed to be the order in which the listeners are notified. Example:

        List mockWithListener = mock(List.class, withSettings().invocationListeners(new YourInvocationListener()));

  See the `listener interface` for more details.

  Specified by:
    `invocationListeners` in interface `MockSettings`
  Parameters:
    `listeners` - The invocation listeners to add. May not be null.
  Returns:
    settings instance so that you can fluently specify other settings

### getInvocationListeners

    public java.util.List<InvocationListener> getInvocationListeners()

  Description copied from interface: `MockCreationSettings`
  the invocation listeners attached to this mock, see `MockSettings.invocationListeners(org.mockito.listeners.InvocationListener...)`.

  Specified by:
    `getInvocationListeners` in interface `MockCreationSettings<T>`
  Overrides:
    `getInvocationListeners` in class `CreationSettings<T>`

### hasInvocationListeners

    public boolean hasInvocationListeners()

### getTypeToMock

    public java.lang.Class<T> getTypeToMock()

  Description copied from interface: `MockCreationSettings`
  Mocked type. An interface or class the mock should implement / extend.

  Specified by:
    `getTypeToMock` in interface `MockCreationSettings<T>`
  Overrides:
    `getTypeToMock` in class `CreationSettings<T>`

### confirm

    public MockCreationSettings<T> confirm(java.lang.Class<T> typeToMock)

Overview | Package | Class | Use | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD