private MockHandler<Object> mockHandler() { assertGoodMock(); return MockUtil.getMockHandler(toInspect); }
public static MockCreationSettings getMockSettings(Object mock) { return getMockHandler(mock).getMockSettings(); } }
public static InvocationContainerImpl getInvocationContainer(Object mock) { return (InvocationContainerImpl) getMockHandler(mock).getInvocationContainer(); }
public static MockName getMockName(Object mock) { return getMockHandler(mock).getMockSettings().getMockName(); }
public <T> VoidMethodStubbable<T> stubVoid(T mock) { InternalMockHandler<T> handler = mockUtil.getMockHandler(mock); mockingProgress.stubbingStarted(); return handler.voidMethodStubbable(mock); }
public MockName getMockName(Object mock) { return getMockHandler(mock).getMockSettings().getMockName(); }
public static <T> void resetMock(T mock) { MockHandler oldHandler = getMockHandler(mock); MockCreationSettings settings = oldHandler.getMockSettings(); MockHandler newHandler = createMockHandler(settings); mockMaker.resetMock(mock, newHandler, settings); }
private void assertNotStubOnlyMock(Object mock) { if (getMockHandler(mock).getMockSettings().isStubOnly()) { throw stubPassedToVerify(mock); } }
protected GenericMetadataSupport actualParameterizedType(Object mock) { CreationSettings mockSettings = (CreationSettings) MockUtil.getMockHandler(mock).getMockSettings(); return GenericMetadataSupport.inferFrom(mockSettings.getTypeToMock()); }
public Object answer(InvocationOnMock invocation) throws Throwable { Class<?> methodReturnType = invocation.getMethod().getReturnType(); Object mock = invocation.getMock(); Class<?> mockType = MockUtil.getMockHandler(mock).getMockSettings().getTypeToMock(); if (methodReturnType.isAssignableFrom(mockType)) { return invocation.getMock(); } return defaultReturn.returnValueFor(methodReturnType); }
public void maybeRedefineMockName(Object mock, String newName) { MockName mockName = getMockName(mock); //TODO SF hacky... if (mockName.isDefault() && getMockHandler(mock).getMockSettings() instanceof CreationSettings) { ((CreationSettings) getMockHandler(mock).getMockSettings()).setMockName(new MockNameImpl(newName)); } } }
/** * gets all invocations from mocks. Invocations are ordered earlier first. * * @param mocks mocks * @return invocations */ public List<Invocation> find(List<?> mocks) { Set<Invocation> invocationsInOrder = new TreeSet<Invocation>(new SequenceNumberComparator()); for (Object mock : mocks) { InternalMockHandler<Object> handler = new MockUtil().getMockHandler(mock); List<Invocation> fromSingleMock = handler.getInvocationContainer().getInvocations(); invocationsInOrder.addAll(fromSingleMock); } return new LinkedList<Invocation>(invocationsInOrder); }
public <T> void resetMock(T mock) { InternalMockHandler oldHandler = (InternalMockHandler) getMockHandler(mock); MockCreationSettings settings = oldHandler.getMockSettings(); MockHandler newHandler = new MockHandlerFactory().create(settings); mockMaker.resetMock(mock, newHandler, settings); }
public static void maybeRedefineMockName(Object mock, String newName) { MockName mockName = getMockName(mock); //TODO SF hacky... MockCreationSettings mockSettings = getMockHandler(mock).getMockSettings(); if (mockName.isDefault() && mockSettings instanceof CreationSettings) { ((CreationSettings) mockSettings).setMockName(new MockNameImpl(newName)); } }
/** * Verify the same invocations have been applied to two mocks. This is generally not * the preferred way test with mockito and should be avoided if possible. * @param expected the mock containing expected invocations * @param actual the mock containing actual invocations * @param argumentAdapters adapters that can be used to change argument values before they are compared */ public static <T> void verifySameInvocations(T expected, T actual, InvocationArgumentsAdapter... argumentAdapters) { List<Invocation> expectedInvocations = ((InvocationContainerImpl) MockUtil.getMockHandler(expected).getInvocationContainer()).getInvocations(); List<Invocation> actualInvocations = ((InvocationContainerImpl) MockUtil.getMockHandler(actual).getInvocationContainer()).getInvocations(); verifySameInvocations(expectedInvocations, actualInvocations, argumentAdapters); }
public Object[] ignoreStubs(Object... mocks) { for (Object m : mocks) { InvocationContainer invocationContainer = new MockUtil().getMockHandler(m).getInvocationContainer(); List<Invocation> ins = invocationContainer.getInvocations(); for (Invocation in : ins) { if (in.stubInfo() != null) { in.ignoreForVerification(); } } } return mocks; }
public <T> T when(T mock) { MockUtil mockUtil = new MockUtil(); if (mock == null) { reporter.nullPassedToWhenMethod(); } else { if (!mockUtil.isMock(mock)) { reporter.notAMockPassedToWhenMethod(); } } mockUtil.getMockHandler(mock).setAnswersForStubbing(answers); return mock; }
/** * Retrieve the expected type when it came from a primitive. If the type cannot be retrieve, return null. * * @param invocation the current invocation * @param returnType the expected return type * @return the type or null if not found */ private Class<?> findTypeFromGeneric(final InvocationOnMock invocation, final TypeVariable returnType) { // Class level final MockCreationSettings mockSettings = MockUtil.getMockHandler(invocation.getMock()).getMockSettings(); final GenericMetadataSupport returnTypeSupport = GenericMetadataSupport .inferFrom(mockSettings.getTypeToMock()) .resolveGenericReturnType(invocation.getMethod()); final Class<?> rawType = returnTypeSupport.rawType(); // Method level if (rawType == Object.class) { return findTypeFromGenericInArguments(invocation, returnType); } return rawType; }
private Object getMock(InvocationOnMock invocation) throws Throwable { InternalMockHandler<Object> handler = new MockUtil().getMockHandler(invocation.getMock()); InvocationContainerImpl container = (InvocationContainerImpl) handler.getInvocationContainer(); // matches invocation for verification for (StubbedInvocationMatcher stubbedInvocationMatcher : container.getStubbedInvocations()) { if(container.getInvocationForStubbing().matches(stubbedInvocationMatcher.getInvocation())) { return stubbedInvocationMatcher.answer(invocation); } } // deep stub return recordDeepStubMock(invocation, container); }
public void verifyNoMoreInteractions(Object... mocks) { assertMocksNotEmpty(mocks); mockingProgress.validateState(); for (Object mock : mocks) { try { if (mock == null) { reporter.nullPassedToVerifyNoMoreInteractions(); } InvocationContainer invocations = mockUtil.getMockHandler(mock).getInvocationContainer(); VerificationDataImpl data = new VerificationDataImpl(invocations, null); VerificationModeFactory.noMoreInteractions().verify(data); } catch (NotAMockException e) { reporter.notAMockPassedToVerifyNoMoreInteractions(); } } }