Package org.opensearch.mockito.plugin
Class PriviledgedMockMaker
java.lang.Object
org.opensearch.mockito.plugin.PriviledgedMockMaker
- All Implemented Interfaces:
org.mockito.plugins.MockMaker
public class PriviledgedMockMaker
extends java.lang.Object
implements org.mockito.plugins.MockMaker
Mockito plugin which wraps the Mockito calls into priviledged execution blocks and respects
SecurityManager presence.
-
Nested Class Summary
-
Constructor Summary
Constructors Constructor Description PriviledgedMockMaker()Construct an instance of the priviledged mock maker using ByteBuddyMockMaker under the hood. -
Method Summary
Modifier and Type Method Description voidclearAllCaches()static voidcreateAccessControlContext()Create dedicated AccessControlContext to use the Mockito protection domain (test only) so to relax the security constraints for the test cases which rely on mocks.<T> org.mockito.plugins.MockMaker.ConstructionMockControl<T>createConstructionMock(java.lang.Class<T> type, java.util.function.Function<org.mockito.MockedConstruction.Context,org.mockito.mock.MockCreationSettings<T>> settingsFactory, java.util.function.Function<org.mockito.MockedConstruction.Context,org.mockito.invocation.MockHandler<T>> handlerFactory, org.mockito.MockedConstruction.MockInitializer<T> mockInitializer)<T> TcreateMock(org.mockito.mock.MockCreationSettings<T> settings, org.mockito.invocation.MockHandler handler)<T> java.util.Optional<T>createSpy(org.mockito.mock.MockCreationSettings<T> settings, org.mockito.invocation.MockHandler handler, T object)<T> org.mockito.plugins.MockMaker.StaticMockControl<T>createStaticMock(java.lang.Class<T> type, org.mockito.mock.MockCreationSettings<T> settings, org.mockito.invocation.MockHandler handler)org.mockito.invocation.MockHandlergetHandler(java.lang.Object mock)org.mockito.plugins.MockMaker.TypeMockabilityisTypeMockable(java.lang.Class<?> type)voidresetMock(java.lang.Object mock, org.mockito.invocation.MockHandler newHandler, org.mockito.mock.MockCreationSettings settings)
-
Constructor Details
-
PriviledgedMockMaker
public PriviledgedMockMaker()Construct an instance of the priviledged mock maker using ByteBuddyMockMaker under the hood.
-
-
Method Details
-
createAccessControlContext
public static void createAccessControlContext()Create dedicated AccessControlContext to use the Mockito protection domain (test only) so to relax the security constraints for the test cases which rely on mocks. This plugin wraps the mock/spy creation into priviledged action using the custom access control context since Mockito does not support SecurityManager out of the box. The method has to be called by test framework before the SecurityManager is being set, otherwise additional permissions have to be granted to the caller: permission java.security.Permission "createAccessControlContext" -
createMock
public <T> T createMock(org.mockito.mock.MockCreationSettings<T> settings, org.mockito.invocation.MockHandler handler)- Specified by:
createMockin interfaceorg.mockito.plugins.MockMaker
-
createSpy
public <T> java.util.Optional<T> createSpy(org.mockito.mock.MockCreationSettings<T> settings, org.mockito.invocation.MockHandler handler, T object)- Specified by:
createSpyin interfaceorg.mockito.plugins.MockMaker
-
getHandler
public org.mockito.invocation.MockHandler getHandler(java.lang.Object mock)- Specified by:
getHandlerin interfaceorg.mockito.plugins.MockMaker
-
resetMock
public void resetMock(java.lang.Object mock, org.mockito.invocation.MockHandler newHandler, org.mockito.mock.MockCreationSettings settings)- Specified by:
resetMockin interfaceorg.mockito.plugins.MockMaker
-
isTypeMockable
@Incubating public org.mockito.plugins.MockMaker.TypeMockability isTypeMockable(java.lang.Class<?> type)- Specified by:
isTypeMockablein interfaceorg.mockito.plugins.MockMaker
-
createStaticMock
public <T> org.mockito.plugins.MockMaker.StaticMockControl<T> createStaticMock(java.lang.Class<T> type, org.mockito.mock.MockCreationSettings<T> settings, org.mockito.invocation.MockHandler handler)- Specified by:
createStaticMockin interfaceorg.mockito.plugins.MockMaker
-
createConstructionMock
public <T> org.mockito.plugins.MockMaker.ConstructionMockControl<T> createConstructionMock(java.lang.Class<T> type, java.util.function.Function<org.mockito.MockedConstruction.Context,org.mockito.mock.MockCreationSettings<T>> settingsFactory, java.util.function.Function<org.mockito.MockedConstruction.Context,org.mockito.invocation.MockHandler<T>> handlerFactory, org.mockito.MockedConstruction.MockInitializer<T> mockInitializer)- Specified by:
createConstructionMockin interfaceorg.mockito.plugins.MockMaker
-
clearAllCaches
public void clearAllCaches()- Specified by:
clearAllCachesin interfaceorg.mockito.plugins.MockMaker
-