Class InlineByteBuddyMockMaker

java.lang.Object
org.mockito.internal.creation.bytebuddy.InlineByteBuddyMockMaker
All Implemented Interfaces:
Instantiator, ClassCreatingMockMaker, InlineMockMaker, MockMaker

@Incubating public class InlineByteBuddyMockMaker extends Object implements ClassCreatingMockMaker, InlineMockMaker, Instantiator
  • Field Details

  • Constructor Details

    • InlineByteBuddyMockMaker

      public InlineByteBuddyMockMaker()
    • InlineByteBuddyMockMaker

      InlineByteBuddyMockMaker(InlineDelegateByteBuddyMockMaker inlineDelegateByteBuddyMockMaker)
  • Method Details

    • newInstance

      public <T> T newInstance(Class<T> cls)
      Description copied from interface: Instantiator
      Creates instance of given class
      Specified by:
      newInstance in interface Instantiator
    • createMockType

      public <T> Class<? extends T> createMockType(MockCreationSettings<T> settings)
      Specified by:
      createMockType in interface ClassCreatingMockMaker
    • clearMock

      public void clearMock(Object mock)
      Description copied from interface: InlineMockMaker
      Clean up internal state for specified mock. You may assume there won't be any interaction to the specific mock after this is called.
      Specified by:
      clearMock in interface InlineMockMaker
      Parameters:
      mock - the mock instance whose internal state is to be cleaned.
    • clearAllMocks

      public void clearAllMocks()
      Description copied from interface: InlineMockMaker
      Cleans up internal state for all existing mocks. You may assume there won't be any interaction to mocks created previously after this is called.
      Specified by:
      clearAllMocks in interface InlineMockMaker
    • createMock

      public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler)
      Description copied from interface: MockMaker
      If you want to provide your own implementation of MockMaker this method should:
      • Create a proxy object that implements settings.typeToMock and potentially also settings.extraInterfaces.
      • You may use the information from settings to create/configure your proxy object.
      • Your proxy object should carry the handler with it. For example, if you generate byte code to create the proxy you could generate an extra field to keep the handler with the generated object. Your implementation of MockMaker is required to provide this instance of handler when MockMaker.getHandler(Object) is called.
      Specified by:
      createMock in interface MockMaker
      Type Parameters:
      T - Type of the mock to return, actually the settings.getTypeToMock.
      Parameters:
      settings - Mock creation settings like type to mock, extra interfaces and so on.
      handler - See MockHandler. Do not provide your own implementation at this time. Make sure your implementation of MockMaker.getHandler(Object) will return this instance.
      Returns:
      The mock instance.
    • createSpy

      public <T> Optional<T> createSpy(MockCreationSettings<T> settings, MockHandler handler, T instance)
      Description copied from interface: MockMaker
      By implementing this method, a mock maker can optionally support the creation of spies where all fields are set within a constructor. This avoids problems when creating spies of classes that declare effectively final instance fields where setting field values from outside the constructor is prohibited.
      Specified by:
      createSpy in interface MockMaker
      Type Parameters:
      T - Type of the mock to return, actually the settings.getTypeToMock.
      Parameters:
      settings - Mock creation settings like type to mock, extra interfaces and so on.
      handler - See MockHandler. Do not provide your own implementation at this time. Make sure your implementation of MockMaker.getHandler(Object) will return this instance.
      instance - The object to spy upon.
      Returns:
      The spy instance, if this mock maker supports direct spy creation.
    • getHandler

      public MockHandler getHandler(Object mock)
      Description copied from interface: MockMaker
      Returns the handler for the mock. Do not provide your own implementations at this time because the work on the MockHandler api is not completed. Use the instance provided to you by Mockito at MockMaker.createMock(org.mockito.mock.MockCreationSettings<T>, org.mockito.invocation.MockHandler) or MockMaker.resetMock(java.lang.Object, org.mockito.invocation.MockHandler, org.mockito.mock.MockCreationSettings).
      Specified by:
      getHandler in interface MockMaker
      Parameters:
      mock - The mock instance.
      Returns:
      The mock handler, but may return null - it means that there is no handler attached to provided object. This means the passed object is not really a Mockito mock.
    • resetMock

      public void resetMock(Object mock, MockHandler newHandler, MockCreationSettings settings)
      Description copied from interface: MockMaker
      Replaces the existing handler on mock with newHandler.

      The invocation handler actually store invocations to achieve stubbing and verification. In order to reset the mock, we pass a new instance of the invocation handler.

      Your implementation should make sure the newHandler is correctly associated to passed mock

      Specified by:
      resetMock in interface MockMaker
      Parameters:
      mock - The mock instance whose invocation handler is to be replaced.
      newHandler - The new invocation handler instance.
      settings - The mock settings - should you need to access some of the mock creation details.
    • isTypeMockable

      public MockMaker.TypeMockability isTypeMockable(Class<?> type)
      Description copied from interface: MockMaker
      Indicates if the given type can be mocked by this mockmaker.

      Mockmaker may have different capabilities in term of mocking, typically Mockito 1.x's internal mockmaker cannot mock final types. Other implementations, may have different limitations.

      Specified by:
      isTypeMockable in interface MockMaker
      Parameters:
      type - The type inspected for mockability.
      Returns:
      object that carries the information about mockability of given type.
    • createStaticMock

      public <T> MockMaker.StaticMockControl<T> createStaticMock(Class<T> type, MockCreationSettings<T> settings, MockHandler handler)
      Description copied from interface: MockMaker
      If you want to provide your own implementation of MockMaker this method should:
      • Alter the supplied class to only change its behavior in the current thread.
      • Only alters the static method's behavior after being enabled.
      • Stops the altered behavior when disabled.
      Specified by:
      createStaticMock in interface MockMaker
      Type Parameters:
      T - Type of the mock to return, actually the settings.getTypeToMock.
      settings - Mock creation settings like type to mock, extra interfaces and so on.
      handler - See MockHandler. Do not provide your own implementation at this time. Make sure your implementation of MockMaker.getHandler(Object) will return this instance.
      Returns:
      A control for the static mock.
    • createConstructionMock

      public <T> MockMaker.ConstructionMockControl<T> createConstructionMock(Class<T> type, Function<MockedConstruction.Context,MockCreationSettings<T>> settingsFactory, Function<MockedConstruction.Context,MockHandler<T>> handlerFactory, MockedConstruction.MockInitializer<T> mockInitializer)
      Description copied from interface: MockMaker
      If you want to provide your own implementation of MockMaker this method should:
      • Intercept all constructions of the specified type in the current thread
      • Only intercept the construction after being enabled.
      • Stops the interception when disabled.
      Specified by:
      createConstructionMock in interface MockMaker
      Type Parameters:
      T - Type of the mock to return, actually the settings.getTypeToMock.
      settingsFactory - Factory for mock creation settings like type to mock, extra interfaces and so on.
      handlerFactory - Factory for settings. See MockHandler. Do not provide your own implementation at this time. Make sure your implementation of MockMaker.getHandler(Object) will return this instance.
      Returns:
      A control for the mocked construction.
    • clearAllCaches

      public void clearAllCaches()
      Description copied from interface: MockMaker
      Clears all cashes for mocked types and removes all byte code alterations, if possible.
      Specified by:
      clearAllCaches in interface MockMaker