@Override @SuppressWarnings("unchecked") public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) { Class<T> typeToMock = settings.getTypeToMock(); @SuppressWarnings("rawtypes") Set<Class> interfacesSet = settings.getExtraInterfaces(); Class<?>[] extraInterfaces = interfacesSet.toArray(new Class[interfacesSet.size()]); InvocationHandler invocationHandler = new InvocationHandlerAdapter(handler); if (typeToMock.isInterface()) { // support interfaces via java.lang.reflect.Proxy @SuppressWarnings("rawtypes") Class[] classesToMock = new Class[extraInterfaces.length + 1]; classesToMock[0] = typeToMock; System.arraycopy(extraInterfaces, 0, classesToMock, 1, extraInterfaces.length); ClassLoader contextClassLoader = Thread.currentThread().getContextClassLoader(); // newProxyInstance returns the type of typeToMock T mock = (T) Proxy.newProxyInstance(contextClassLoader, classesToMock, invocationHandler); return mock; } else { try { Class<? extends T> proxyClass = getProxyClass(typeToMock, extraInterfaces); T mock = proxyClass.newInstance(); ((ClassProxy) mock).setHandler(invocationHandler); return mock; } catch (RuntimeException e) { throw e; } catch (Exception e) { throw new MockitoException("Failed to mock " + typeToMock, e); } } }
public <T> T createMock(MockCreationSettings<T> settings, MockHandler handler) { InternalMockHandler mockitoHandler = cast(handler); return ClassImposterizer.INSTANCE.imposterise( new MethodInterceptorForGroovyFilter(mockitoHandler, settings), settings.getTypeToMock(), settings.getExtraInterfaces()); }
public Object handle(Invocation invocation) throws Throwable { if (invocationContainerImpl.hasAnswersForStubbing()) { invocation.setPhase(InvocationPhase.DEFINE); // stubbing voids with stubVoid() or doAnswer() style InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(mockingProgress.getArgumentMatcherStorage(), invocation); invocationContainerImpl.setMethodForStubbing(invocationMatcher); return null; } VerificationMode verificationMode = mockingProgress.pullVerificationMode(); InvocationMatcher invocationMatcher = matchersBinder.bindMatchers(mockingProgress.getArgumentMatcherStorage(), invocation); mockingProgress.validateState(); // if verificationMode is not null then someone is doing verify() if (verificationMode != null) { invocation.setPhase(InvocationPhase.VERIFY); // We need to check if verification was started on the correct mock // - see VerifyingWithAnExtraCallToADifferentMockTest (bug 138) if (((MockAwareVerificationMode) verificationMode).getMock() == invocation.getMock()) { VerificationDataImpl data = createVerificationData(invocationContainerImpl, invocationMatcher); verificationMode.verify(data); return null; } else { // this means there is an invocation on a different mock. Re-adding verification mode // - see VerifyingWithAnExtraCallToADifferentMockTest (bug 138) mockingProgress.verificationStarted(verificationMode); } } // prepare invocation for stubbing invocationContainerImpl.setInvocationForPotentialStubbing(invocationMatcher); OngoingStubbingImpl<T> ongoingStubbing = new OngoingStubbingImpl<T>(invocationContainerImpl); mockingProgress.reportOngoingStubbing(ongoingStubbing); // look for existing answer for this invocation StubbedInvocationMatcher stubbedInvocation = invocationContainerImpl.findAnswerFor(invocation); if (stubbedInvocation != null) { invocation.setPhase(InvocationPhase.EXECUTE); stubbedInvocation.captureArgumentsFrom(invocation); return stubbedInvocation.answer(invocation); } else { invocation.setPhase(InvocationPhase.DEFINE); Object ret = mockSettings.getDefaultAnswer().answer(invocation); // redo setting invocation for potential stubbing in case of partial // mocks / spies. // Without it, the real method inside 'when' might have delegated // to other self method and overwrite the intended stubbed method // with a different one. The reset is required to avoid runtime exception that validates // return type with stubbed method signature. invocationContainerImpl.resetInvocationForPotentialStubbing(invocationMatcher); return ret; } }
private VerificationDataImpl createVerificationData( InvocationContainerImpl invocationContainerImpl, InvocationMatcher invocationMatcher) { if (mockSettings.isStubOnly()) { new Reporter().stubPassedToVerify(); // this throws an exception } return new VerificationDataImpl(invocationContainerImpl, invocationMatcher); }
@Test public void createMock() throws Exception { MockDefinition definition = new MockDefinition( "name", EXAMPLE_SERVICE_TYPE, new Class<?>[] {ExampleExtraInterface.class}, Answers.RETURNS_SMART_NULLS, true, MockReset.BEFORE, null); ExampleService mock = definition.createMock(); MockCreationSettings<?> settings = SpringBootMockUtil.getMockSettings(mock); assertThat(mock).isInstanceOf(ExampleService.class); assertThat(mock).isInstanceOf(ExampleExtraInterface.class); assertThat(settings.getMockName().toString()).isEqualTo("name"); assertThat(settings.getDefaultAnswer()).isEqualTo(Answers.RETURNS_SMART_NULLS.get()); assertThat(settings.isSerializable()).isTrue(); assertThat(MockReset.get(mock)).isEqualTo(MockReset.BEFORE); }