Search in sources :

Example 11 with MockHandler

use of org.mockito.invocation.MockHandler in project powermock by powermock.

the class PowerMockitoStubberImpl method addAnswersForStubbing.

@SuppressWarnings("unchecked")
private void addAnswersForStubbing(MockitoMethodInvocationControl invocationControl) {
    final MockHandler mockHandler = invocationControl.getInvocationHandler().getHandler();
    final List list = Whitebox.getInternalState(this, List.class);
    try {
        Whitebox.invokeMethod(mockHandler, "setAnswersForStubbing", list);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : MockHandler(org.mockito.invocation.MockHandler) List(java.util.List)

Example 12 with MockHandler

use of org.mockito.invocation.MockHandler in project mockito by mockito.

the class MockUtil method getMockHandler.

public static MockHandler<?> getMockHandler(Object mock) {
    if (mock == null) {
        throw new NotAMockException("Argument should be a mock, but is null!");
    }
    mock = resolve(mock);
    MockHandler handler = mockMaker.getHandler(mock);
    if (handler != null) {
        return handler;
    } else {
        throw new NotAMockException("Argument should be a mock, but is: " + mock.getClass());
    }
}
Also used : NotAMockException(org.mockito.exceptions.misusing.NotAMockException) MockHandlerFactory.createMockHandler(org.mockito.internal.handler.MockHandlerFactory.createMockHandler) MockHandler(org.mockito.invocation.MockHandler)

Example 13 with MockHandler

use of org.mockito.invocation.MockHandler in project mockito by mockito.

the class MockitoCore method verify.

public <T> T verify(T mock, VerificationMode mode) {
    if (mock == null) {
        throw nullPassedToVerify();
    }
    MockingDetails mockingDetails = mockingDetails(mock);
    if (!mockingDetails.isMock()) {
        throw notAMockPassedToVerify(mock.getClass());
    }
    assertNotStubOnlyMock(mock);
    MockHandler handler = mockingDetails.getMockHandler();
    mock = (T) VerificationStartedNotifier.notifyVerificationStarted(handler.getMockSettings().getVerificationStartedListeners(), mockingDetails);
    MockingProgress mockingProgress = mockingProgress();
    VerificationMode actualMode = mockingProgress.maybeVerifyLazily(mode);
    mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, actualMode, mockingProgress.verificationListeners()));
    return mock;
}
Also used : MockAwareVerificationMode(org.mockito.internal.verification.MockAwareVerificationMode) DefaultMockingDetails(org.mockito.internal.util.DefaultMockingDetails) MockingDetails(org.mockito.MockingDetails) MockUtil.getMockHandler(org.mockito.internal.util.MockUtil.getMockHandler) MockHandler(org.mockito.invocation.MockHandler) MockingProgress(org.mockito.internal.progress.MockingProgress) MockAwareVerificationMode(org.mockito.internal.verification.MockAwareVerificationMode) VerificationMode(org.mockito.verification.VerificationMode)

Example 14 with MockHandler

use of org.mockito.invocation.MockHandler in project mockito by mockito.

the class MockedStaticImpl method verify.

@Override
public void verify(Verification verification, VerificationMode mode) {
    assertNotClosed();
    MockingDetails mockingDetails = Mockito.mockingDetails(control.getType());
    MockHandler handler = mockingDetails.getMockHandler();
    VerificationStartedNotifier.notifyVerificationStarted(handler.getMockSettings().getVerificationStartedListeners(), mockingDetails);
    MockingProgress mockingProgress = mockingProgress();
    VerificationMode actualMode = mockingProgress.maybeVerifyLazily(mode);
    mockingProgress.verificationStarted(new MockAwareVerificationMode(control.getType(), actualMode, mockingProgress.verificationListeners()));
    try {
        verification.apply();
    } catch (MockitoException | MockitoAssertionError e) {
        throw e;
    } catch (Throwable t) {
        throw new MockitoException(join("An unexpected error occurred while verifying a static stub", "", "To correctly verify a stub, invoke a single static method of " + control.getType().getName() + " in the provided lambda.", "For example, if a method 'sample' was defined, provide a lambda or anonymous class containing the code", "", "() -> " + control.getType().getSimpleName() + ".sample()", "or", control.getType().getSimpleName() + "::sample"), t);
    }
}
Also used : MockAwareVerificationMode(org.mockito.internal.verification.MockAwareVerificationMode) MockitoAssertionError(org.mockito.exceptions.base.MockitoAssertionError) MockitoException(org.mockito.exceptions.base.MockitoException) MockingDetails(org.mockito.MockingDetails) MockHandler(org.mockito.invocation.MockHandler) MockingProgress(org.mockito.internal.progress.MockingProgress) MockAwareVerificationMode(org.mockito.internal.verification.MockAwareVerificationMode) VerificationMode(org.mockito.verification.VerificationMode)

Aggregations

MockHandler (org.mockito.invocation.MockHandler)14 Test (org.junit.Test)5 InternalMockHandler (org.mockito.internal.InternalMockHandler)5 MockHandlerFactory.createMockHandler (org.mockito.internal.handler.MockHandlerFactory.createMockHandler)4 MockitoAssertionError (org.mockito.exceptions.base.MockitoAssertionError)3 NotAMockException (org.mockito.exceptions.misusing.NotAMockException)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 MockingDetails (org.mockito.MockingDetails)2 MockHandlerImpl (org.mockito.internal.handler.MockHandlerImpl)2 MockingProgress (org.mockito.internal.progress.MockingProgress)2 MockAwareVerificationMode (org.mockito.internal.verification.MockAwareVerificationMode)2 MockCreationSettings (org.mockito.mock.MockCreationSettings)2 VerificationMode (org.mockito.verification.VerificationMode)2 List (java.util.List)1 MockitoException (org.mockito.exceptions.base.MockitoException)1 AbstractMockMakerTest (org.mockito.internal.creation.AbstractMockMakerTest)1 DelegatingMethod (org.mockito.internal.creation.DelegatingMethod)1 LocationImpl (org.mockito.internal.debugging.LocationImpl)1 InvocationImpl (org.mockito.internal.invocation.InvocationImpl)1 CleanTraceRealMethod (org.mockito.internal.invocation.realmethod.CleanTraceRealMethod)1