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