Refine search
/** * 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 void cannotInjectDependency(Field field, Object matchingMock, Exception details) { throw new MockitoException(join( "Mockito couldn't inject mock dependency '" + new MockUtil().getMockName(matchingMock) + "' on field ", "'" + field + "'", "whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.", "Also I failed because: " + details.getCause().getMessage(), "" ), details); }
@Override public String toString() { return "HashCodeAndEqualsMockWrapper{" + "mockInstance=" + (MockUtil.isMock(mockInstance) ? MockUtil.getMockName(mockInstance) : typeInstanceString()) + '}'; }
@Override public String toString() { MockUtil mockUtil = new MockUtil(); return "HashCodeAndEqualsMockWrapper{" + "mockInstance=" + (mockUtil.isMock(mockInstance) ? mockUtil.getMockName(mockInstance) : typeInstanceString()) + '}'; }
if (new MockUtil().isMock(instance)) { throw new MockitoException("Problems initiating spied field " + field.getName(), e);
@Override public void process(Class<?> context, Object testInstance) { Field[] fields = context.getDeclaredFields(); for (Field field : fields) { if (field.isAnnotationPresent(Spy.class) && !field.isAnnotationPresent(InjectMocks.class)) { assertNoIncompatibleAnnotations(Spy.class, field, Mock.class, Captor.class); field.setAccessible(true); Object instance; try { instance = field.get(testInstance); if (MockUtil.isMock(instance)) { // instance has been spied earlier // for example happens when MockitoAnnotations.initMocks is called two times. Mockito.reset(instance); } else if (instance != null) { field.set(testInstance, spyInstance(field, instance)); } else { field.set(testInstance, spyNewInstance(testInstance, field)); } } catch (Exception e) { throw new MockitoException("Unable to initialize @Spy annotated field '" + field.getName() + "'.\n" + e.getMessage(), e); } } } }
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; }
@Override public boolean isMock(){ return MockUtil.isMock(toInspect); }
public static MockitoException delegatedMethodDoesNotExistOnDelegate(Method mockMethod, Object mock, Object delegate) { return new MockitoException(join( "Methods called on mock must exist in delegated instance.", "When calling: " + mockMethod + " on mock: " + MockUtil.getMockName(mock), "no such method was found.", "Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods", "(delegate instance had type: " + delegate.getClass().getSimpleName() + ")" )); }
@SuppressWarnings({"unchecked"}) private <T> T registerInBeanContainer(T bean, Annotation... qualifiers) { final Class<T> clazz; if (MockUtil.isMock(bean) || MockUtil.isSpy(bean)) { clazz = MockUtil.getMockHandler(bean).getMockSettings().getTypeToMock(); } else { clazz = (Class<T>) bean.getClass(); } Class<?>[] types = Arrays.copyOf(clazz.getInterfaces(), clazz.getInterfaces().length + 1); types[types.length - 1] = clazz; for (Class type : types) { putInTestBeanContainer(bean, type, qualifiers); if (qualifiers.length > 1) { Stream.of(qualifiers).forEach(q -> putInTestBeanContainer(bean, type, q)); } } return bean; }
public void wrongTypeOfArgumentToReturn(InvocationOnMock invocation, String expectedType, Class actualType, int argumentIndex) { throw new WrongTypeOfReturnValue(join( "The argument of type '" + actualType.getSimpleName() + "' cannot be returned because the following ", "method should return the type '" + expectedType + "'", " -> " + new MockUtil().getMockName(invocation.getMock()) + "." + invocation.getMethod().getName() + "()", "", "The reason for this error can be :", "1. The wanted argument position is incorrect.", "2. The answer is used on the wrong interaction.", "", "Position of the wanted argument is " + argumentIndex + " and " + possibleArgumentTypesOf(invocation), "***", "However if you're still unsure why you're getting above error read on.", "Due to the nature of the syntax above problem might occur because:", "1. This exception *might* occur in wrongly written multi-threaded tests.", " Please refer to Mockito FAQ on limitations of concurrency testing.", "2. A spy is stubbed using when(spy.foo()).then() syntax. It is safer to stub spies - ", " - with doReturn|Throw() family of methods. More in javadocs for Mockito.spy() method.", "" )); } }
public static MockCreationSettings getMockSettings(Object mock) { return getMockHandler(mock).getMockSettings(); } }
public <T> T verify(T mock, VerificationMode mode) { if (mock == null) { reporter.nullPassedToVerify(); } else if (!mockUtil.isMock(mock)) { reporter.notAMockPassedToVerify(mock.getClass()); } mockingProgress.verificationStarted(new MockAwareVerificationMode(mock, mode)); return mock; }
public static boolean isSpy(Object mock) { return isMock(mock) && getMockSettings(mock).getDefaultAnswer() == Mockito.CALLS_REAL_METHODS; }
MockUtil mockUtil = new MockUtil(); MockName mockName = mockUtil.getMockName(mockitoMock); String mockedType = mockUtil.getMockSettings(mockitoMock).getTypeToMock().getCanonicalName(); throw new MockitoSerializationIssue(join( "The mock '" + mockName + "' of type '" + mockedType + "'",
@Override public <T> T when(T mock) { if (mock == null) { throw nullPassedToWhenMethod(); } if (!isMock(mock)) { throw notAMockPassedToWhenMethod(); } MockUtil.getInvocationContainer(mock).setAnswersForStubbing(answers, strictness); return mock; }
private List<Object> selectMatchingName(Collection<Object> mocks, Field candidateFieldToBeInjected) { List<Object> mockNameMatches = new ArrayList<Object>(); for (Object mock : mocks) { if (candidateFieldToBeInjected.getName().equals(getMockName(mock).toString())) { mockNameMatches.add(mock); } } return mockNameMatches; }