• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2007 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 package org.mockito.invocation;
6 
7 import org.mockito.Incubating;
8 import org.mockito.MockSettings;
9 import org.mockito.mock.MockCreationSettings;
10 
11 import java.io.Serializable;
12 
13 /**
14  * Mockito handler of an invocation on a mock. This is a core part of the API, the heart of Mockito.
15  * See also the {@link org.mockito.plugins.MockMaker}.
16  * Handler can be used to programmatically simulate invocations on the mock object.
17  * <p>
18  * Mockito will provide you with the implementation of this interface via {@link org.mockito.plugins.MockMaker} methods:
19  * {@link org.mockito.plugins.MockMaker#createMock(MockCreationSettings, MockHandler)}
20  * and {@link org.mockito.plugins.MockMaker#resetMock(Object, MockHandler, MockCreationSettings)}.
21  * <p>
22  * You can provide your own implementation of MockHandler but make sure that the right instance is returned by
23  * {@link org.mockito.plugins.MockMaker#getHandler(Object)}.
24  */
25 public interface MockHandler<T> extends Serializable {
26 
27     /**
28      * Takes an invocation object and handles it.
29      * <p>
30      * The default implementation provided by Mockito handles invocations by recording
31      * method calls on mocks for further verification, captures the stubbing information when mock is stubbed,
32      * returns the stubbed values for invocations that have been stubbed, and much more.
33      *
34      * @param invocation The invocation to handle
35      * @return Result
36      * @throws Throwable Throwable
37      */
handle(Invocation invocation)38     Object handle(Invocation invocation) throws Throwable;
39 
40     /**
41      * Read-only settings the mock object was created with.
42      * See {@link org.mockito.Mockito#mock(Class, MockSettings)}
43      *
44      * @return read-only settings of the mock
45      * @since 2.10.0
46      */
47     @Incubating
getMockSettings()48     MockCreationSettings<T> getMockSettings();
49 
50     /**
51      * Returns the object that holds all invocations on the mock object,
52      * including stubbings with declared answers. Do not provide your own implementation.
53      * Returned object is an internal implementation, hidden beneath a public marker interface.
54      * <p>
55      * Please do not provide your own implementation of {@link InvocationContainer} interface at this point.
56      * If you have a use case that requires your own implementation of {@link InvocationContainer}
57      * please reach out to us. You can open a ticket in our issue tracker to start a discussion.
58      *
59      * @return container of invocations, stubbings, and answers of the mock.
60      *          The container is not part of the public API, please do not cast it or provide custom implementations.
61      * @since 2.10.0
62      */
63     @Incubating
getInvocationContainer()64     InvocationContainer getInvocationContainer();
65 }
66