• 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.internal.util;
6 
7 import static org.mockito.internal.handler.MockHandlerFactory.createMockHandler;
8 
9 import org.mockito.Mockito;
10 import org.mockito.exceptions.misusing.NotAMockException;
11 import org.mockito.internal.InternalMockHandler;
12 import org.mockito.internal.configuration.plugins.Plugins;
13 import org.mockito.internal.creation.settings.CreationSettings;
14 import org.mockito.internal.util.reflection.LenientCopyTool;
15 import org.mockito.invocation.MockHandler;
16 import org.mockito.mock.MockCreationSettings;
17 import org.mockito.mock.MockName;
18 import org.mockito.plugins.MockMaker;
19 import org.mockito.plugins.MockMaker.TypeMockability;
20 
21 @SuppressWarnings("unchecked")
22 public class MockUtil {
23 
24     private static final MockMaker mockMaker = Plugins.getMockMaker();
25 
MockUtil()26     private MockUtil() {}
27 
typeMockabilityOf(Class<?> type)28     public static TypeMockability typeMockabilityOf(Class<?> type) {
29       return mockMaker.isTypeMockable(type);
30     }
31 
createMock(MockCreationSettings<T> settings)32     public static <T> T createMock(MockCreationSettings<T> settings) {
33         MockHandler mockHandler =  createMockHandler(settings);
34 
35         T mock = mockMaker.createMock(settings, mockHandler);
36 
37         Object spiedInstance = settings.getSpiedInstance();
38         if (spiedInstance != null) {
39             new LenientCopyTool().copyToMock(spiedInstance, mock);
40         }
41 
42         return mock;
43     }
44 
resetMock(T mock)45     public static <T> void resetMock(T mock) {
46         InternalMockHandler oldHandler = (InternalMockHandler) getMockHandler(mock);
47         MockCreationSettings settings = oldHandler.getMockSettings();
48         MockHandler newHandler = createMockHandler(settings);
49 
50         mockMaker.resetMock(mock, newHandler, settings);
51     }
52 
getMockHandler(T mock)53     public static <T> InternalMockHandler<T> getMockHandler(T mock) {
54         if (mock == null) {
55             throw new NotAMockException("Argument should be a mock, but is null!");
56         }
57 
58         if (isMock(mock)) {
59             MockHandler handler = mockMaker.getHandler(mock);
60             return (InternalMockHandler) handler;
61         } else {
62             throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass());
63         }
64     }
65 
isSpy(Object mock)66     public static boolean isSpy(Object mock) {
67         return isMock(mock) && getMockSettings(mock).getDefaultAnswer() == Mockito.CALLS_REAL_METHODS;
68     }
69 
isMock(Object mock)70     public static boolean isMock(Object mock) {
71         return mock != null && mockMaker.getHandler(mock) != null;
72     }
73 
getMockName(Object mock)74     public static MockName getMockName(Object mock) {
75         return getMockHandler(mock).getMockSettings().getMockName();
76     }
77 
maybeRedefineMockName(Object mock, String newName)78     public static void maybeRedefineMockName(Object mock, String newName) {
79         MockName mockName = getMockName(mock);
80         //TODO SF hacky...
81         MockCreationSettings mockSettings = getMockHandler(mock).getMockSettings();
82 		if (mockName.isDefault() && mockSettings instanceof CreationSettings) {
83             ((CreationSettings) mockSettings).setMockName(new MockNameImpl(newName));
84         }
85     }
86 
getMockSettings(Object mock)87     public static MockCreationSettings getMockSettings(Object mock) {
88         return getMockHandler(mock).getMockSettings();
89     }
90 }
91