1 /*
2  * Copyright (c) 2019 Mockito contributors
3  * This program is made available under the terms of the MIT License.
4  */
5 
6 package org.mockito.plugins;
7 
8 import org.mockito.Incubating;
9 import org.mockito.MockitoFramework;
10 
11 /**
12  * Extension to {@link MockMaker} for mock makers that changes inline method implementations
13  * and need keep track of created mock objects.
14  * <p>
15  * Mockito's default inline mock maker keeps track of created mock objects via weak reference map.
16  * This poses a risk of memory leaks in certain scenarios
17  * (issue <a href="https://github.com/mockito/mockito/pull/1619">#1619</a>).
18  * There is no clean way to tackle those problems at the moment.
19  * Hence, {@code InlineMockMaker} interface exposes methods to explicitly clear mock references.
20  * Those methods are called by {@link MockitoFramework#clearInlineMocks()}.
21  * When the user encounters a leak, he can mitigate the problem with {@link MockitoFramework#clearInlineMocks()}.
22  * <p>
23  * {@code InlineMockMaker} is for expert users and framework integrators, when custom inline mock maker is in use.
24  * If you have a custom {@link MockMaker} that keeps track of mock objects,
25  * please have your mock maker implement {@code InlineMockMaker} interface.
26  * This way, it can participate in {@link MockitoFramework#clearInlineMocks()} API.
27  *
28  * @since 2.25.0
29  */
30 @Incubating
31 public interface InlineMockMaker extends MockMaker {
32 
33     /**
34      * Clean up internal state for specified {@code mock}. You may assume there won't be any interaction to the specific
35      * mock after this is called.
36      *
37      * @param mock the mock instance whose internal state is to be cleaned.
38      * @since 2.25.0
39      */
40     @Incubating
clearMock(Object mock)41     void clearMock(Object mock);
42 
43     /**
44      * Cleans up internal state for all existing mocks. You may assume there won't be any interaction to mocks created
45      * previously after this is called.
46      *
47      * @since 2.25.0
48      */
49     @Incubating
clearAllMocks()50     void clearAllMocks();
51 
52 }
53