Package org.mockito.internal

Examples of org.mockito.internal.MockitoInvocationHandler


        return false;
    }

    private Object performIntercept(MethodInterceptorFilter invocationHandler, final Object interceptionObject,
                                    final Method method, Object[] arguments) throws Throwable {
        MockitoInvocationHandler mockHandler = invocationHandler.getHandler();

        final FilteredCGLIBProxyRealMethod cglibProxyRealMethod = new FilteredCGLIBProxyRealMethod(new RealMethod() {
            private static final long serialVersionUID = 4564320968038564170L;

            public Object invoke(Object target, Object[] arguments) throws Throwable {
                /*
                     * Instruct the MockGateway to don't intercept the next call.
                     * The reason is that when Mockito is spying on objects it
                     * should call the "real method" (which is proxied by Mockito
                     * anyways) so that we don't end up in here one more time which
                     * causes infinite recursion. This should not be done if the
                     * interceptionObject is a final system class because these are
                     * never caught by the Mockito proxy.
                     */
                final Class<?> type = Whitebox.getType(interceptionObject);
                final boolean isFinalSystemClass = type.getName().startsWith("java.")
                        && Modifier.isFinal(type.getModifiers());
                if (!isFinalSystemClass) {
                    MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true);
                }
                try {
                    return method.invoke(target, arguments);
                } catch (InvocationTargetException e) {
                    SafeExceptionRethrower.safeRethrow(e.getCause());
                }
                return null;
            }
        });
        // }
        Invocation invocation = new Invocation(interceptionObject, new DelegatingMethod(method), arguments,
                SequenceNumber.next(), cglibProxyRealMethod) {
            private static final long serialVersionUID = -3679957412502758558L;

            /**
             * We need to override this method because normally the String
             * "method" is assembled by calling the "qualifiedName" method but
             * this is not possible in our case. The reason is that the
             * qualifiedName method does
             *
             * <pre>
             * new MockUtil().getMockName(mock)
             * </pre>
             *
             * which later will call the "isMockitoMock" method which will
             * return false and an exception will be thrown. The reason why
             * "isMockitoMock" returns false is that the mock is not created by
             * the Mockito CGLib Enhancer in case of static methods.
             */
            @Override
            protected String toString(@SuppressWarnings("rawtypes") List<Matcher> matchers, PrintSettings printSettings) {
                MatchersPrinter matchersPrinter = new MatchersPrinter();
                String method = Whitebox.getType(getMock()).getName() + "." + getMethodName();
                String invocation = method + matchersPrinter.getArgumentsLine(matchers, printSettings);
                if (printSettings.isMultiline()
                        || (!matchers.isEmpty() && invocation.length() > Whitebox.<Integer> getInternalState(
                        Invocation.class, "MAX_LINE_LENGTH"))) {
                    return method + matchersPrinter.getArgumentsBlock(matchers, printSettings);
                } else {
                    return invocation;
                }
            }
        };
        try {
            return mockHandler.handle(invocation);
        } catch (MockitoAssertionError e) {
            InvocationControlAssertionError.updateErrorMessageForMethodInvocation(e);
            throw e;
        }
    }
View Full Code Here


        return Mockito.verify(mocks[0]);
    }

    public void verifyNoMoreInteractions() {
        try {
            final MockitoInvocationHandler mockHandler = methodInterceptorFilter.getHandler();
            if (mockHandler instanceof MockHandler<?>) {
                InvocationContainer invocationContainer = ((MockHandler<?>) mockHandler).getInvocationContainer();
                VerificationDataImpl data = new VerificationDataImpl(invocationContainer, null);
                VerificationModeFactory.noMoreInteractions().verify(data);
            } else {
                throw new RuntimeException(
                        "Cannot perform verifyNoMoreInteractions because of unknown mockhandler type "
                                + mockHandler.getClass());
            }
        } catch (MockitoAssertionError e) {
            InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e);
            throw e;
        }
View Full Code Here

    return returnValue;
  }

  @SuppressWarnings("unchecked")
  private void addAnswersForStubbing(MockitoMethodInvocationControl invocationControl) {
    final MockitoInvocationHandler mockHandler = invocationControl.getInvocationHandler().getHandler();
    if (!(mockHandler instanceof MockHandler<?>)) {
      throw new RuntimeException("Cannot perform \"when\" because of unknown mockhandler type " + mockHandler.getClass());
    }
    ((MockHandler<?>) mockHandler).setAnswersForStubbing(Whitebox.getInternalState(this, List.class));
  }
View Full Code Here

        return false;
    }

    private Object performIntercept(MethodInterceptorFilter invocationHandler, final Object interceptionObject,
                                    final Method method, Object[] arguments) throws Throwable {
        MockitoInvocationHandler mockHandler = invocationHandler.getHandler();

        final FilteredCGLIBProxyRealMethod cglibProxyRealMethod = new FilteredCGLIBProxyRealMethod(new RealMethod() {
            private static final long serialVersionUID = 4564320968038564170L;

            public Object invoke(Object target, Object[] arguments) throws Throwable {
                /*
                     * Instruct the MockGateway to don't intercept the next call.
                     * The reason is that when Mockito is spying on objects it
                     * should call the "real method" (which is proxied by Mockito
                     * anyways) so that we don't end up in here one more time which
                     * causes infinite recursion. This should not be done if the
                     * interceptionObject is a final system class because these are
                     * never caught by the Mockito proxy.
                     */
                final Class<?> type = Whitebox.getType(interceptionObject);
                final boolean isFinalSystemClass = type.getName().startsWith("java.")
                        && Modifier.isFinal(type.getModifiers());
                if (!isFinalSystemClass) {
                    MockRepository.putAdditionalState(MockGateway.DONT_MOCK_NEXT_CALL, true);
                }
                try {
                    return method.invoke(target, arguments);
                } catch (InvocationTargetException e) {
                    SafeExceptionRethrower.safeRethrow(e.getCause());
                }
                return null;
            }
        });
        // }
        Invocation invocation = new Invocation(interceptionObject, new DelegatingMethod(method), arguments,
                SequenceNumber.next(), cglibProxyRealMethod) {
            private static final long serialVersionUID = -3679957412502758558L;

            /**
             * We need to override this method because normally the String
             * "method" is assembled by calling the "qualifiedName" method but
             * this is not possible in our case. The reason is that the
             * qualifiedName method does
             *
             * <pre>
             * new MockUtil().getMockName(mock)
             * </pre>
             *
             * which later will call the "isMockitoMock" method which will
             * return false and an exception will be thrown. The reason why
             * "isMockitoMock" returns false is that the mock is not created by
             * the Mockito CGLib Enhancer in case of static methods.
             */
            @Override
            protected String toString(@SuppressWarnings("rawtypes") List<Matcher> matchers, PrintSettings printSettings) {
                MatchersPrinter matchersPrinter = new MatchersPrinter();
                String method = Whitebox.getType(getMock()).getName() + "." + getMethodName();
                String invocation = method + matchersPrinter.getArgumentsLine(matchers, printSettings);
                if (printSettings.isMultiline()
                        || (!matchers.isEmpty() && invocation.length() > Whitebox.<Integer> getInternalState(
                        Invocation.class, "MAX_LINE_LENGTH"))) {
                    return method + matchersPrinter.getArgumentsBlock(matchers, printSettings);
                } else {
                    return invocation;
                }
            }
        };
        try {
            return mockHandler.handle(invocation);
        } catch (NotAMockException e) {
            if(invocation.getMock().getClass().getName().startsWith("java.") &&  MockRepository.getInstanceMethodInvocationControl(invocation.getMock()) != null) {
                return invocation.callRealMethod();
            } else {
                throw e;
View Full Code Here

        return Mockito.verify(mocks[0]);
    }

    public void verifyNoMoreInteractions() {
        try {
            final MockitoInvocationHandler mockHandler = methodInterceptorFilter.getHandler();
            if (mockHandler instanceof MockHandler<?>) {
                InvocationContainer invocationContainer = ((MockHandler<?>) mockHandler).getInvocationContainer();
                VerificationDataImpl data = new VerificationDataImpl(invocationContainer, null);
                VerificationModeFactory.noMoreInteractions().verify(data);
            } else {
                throw new RuntimeException(
                        "Cannot perform verifyNoMoreInteractions because of unknown mockhandler type "
                                + mockHandler.getClass());
            }
        } catch (MockitoAssertionError e) {
            InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e);
            throw e;
        }
View Full Code Here

TOP

Related Classes of org.mockito.internal.MockitoInvocationHandler

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.