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

    Nested classes/interfaces inherited from interface org.mockito.plugins.MockMaker

    org.mockito.plugins.MockMaker.ConstructionMockControl<T extends java.lang.Object>, org.mockito.plugins.MockMaker.StaticMockControl<T extends java.lang.Object>, org.mockito.plugins.MockMaker.TypeMockability
  • 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
    void clearAllCaches()  
    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.
    <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> T createMock​(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.MockHandler getHandler​(java.lang.Object mock)  
    org.mockito.plugins.MockMaker.TypeMockability isTypeMockable​(java.lang.Class<?> type)  
    void resetMock​(java.lang.Object mock, org.mockito.invocation.MockHandler newHandler, org.mockito.mock.MockCreationSettings settings)  

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • 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:
      createMock in interface org.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:
      createSpy in interface org.mockito.plugins.MockMaker
    • getHandler

      public org.mockito.invocation.MockHandler getHandler​(java.lang.Object mock)
      Specified by:
      getHandler in interface org.mockito.plugins.MockMaker
    • resetMock

      public void resetMock​(java.lang.Object mock, org.mockito.invocation.MockHandler newHandler, org.mockito.mock.MockCreationSettings settings)
      Specified by:
      resetMock in interface org.mockito.plugins.MockMaker
    • isTypeMockable

      @Incubating public org.mockito.plugins.MockMaker.TypeMockability isTypeMockable​(java.lang.Class<?> type)
      Specified by:
      isTypeMockable in interface org.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:
      createStaticMock in interface org.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:
      createConstructionMock in interface org.mockito.plugins.MockMaker
    • clearAllCaches

      public void clearAllCaches()
      Specified by:
      clearAllCaches in interface org.mockito.plugins.MockMaker