public MockingDetails mockingDetails(Object toInspect) { return new DefaultMockingDetails(toInspect, new MockUtil()); } }
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); }
public Object answer(InvocationOnMock invocation) throws Throwable { if (new MockUtil().isMock(throwable)) { throw throwable; } Throwable t = throwable.fillInStackTrace(); filter.filter(t); throw t; }
public int invalidArgumentPositionRangeAtInvocationTime(InvocationOnMock invocation, boolean willReturnLastParameter, int argumentIndex) { throw new MockitoException( join("Invalid argument index for the current invocation of method : ", " -> " + new MockUtil().getMockName(invocation.getMock()) + "." + invocation.getMethod().getName() + "()", "", (willReturnLastParameter ? "Last parameter wanted" : "Wanted parameter at position " + argumentIndex) + " but " + possibleArgumentTypesOf(invocation), "The index need to be a positive number that indicates a valid position of the argument in the invocation.", "However it is possible to use the -1 value to indicates that the last argument should be returned.", "")); }
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.", "" )); } }
@Override public String toString() { MockUtil mockUtil = new MockUtil(); return "HashCodeAndEqualsMockWrapper{" + "mockInstance=" + (mockUtil.isMock(mockInstance) ? mockUtil.getMockName(mockInstance) : typeInstanceString()) + '}'; }
/** * 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); }
/** * Creates the wrapper that be used in the serialization stream. * * <p>Immediately serializes the Mockito mock using specifically crafted * {@link AcrossJVMSerializationFeature.MockitoMockObjectOutputStream}, * in a byte array.</p> * * @param mockitoMock The Mockito mock to serialize. * @throws IOException */ public AcrossJVMMockSerializationProxy(Object mockitoMock) throws IOException { ByteArrayOutputStream out = new ByteArrayOutputStream(); ObjectOutputStream objectOutputStream = new MockitoMockObjectOutputStream(out); objectOutputStream.writeObject(mockitoMock); objectOutputStream.close(); out.close(); MockCreationSettings mockSettings = new MockUtil().getMockSettings(mockitoMock); this.serializedMock = out.toByteArray(); this.typeToMock = mockSettings.getTypeToMock(); this.extraInterfaces = mockSettings.getExtraInterfaces(); }
public String print(List<Matcher> matchers, Invocation invocation) { MatchersPrinter matchersPrinter = new MatchersPrinter(); String qualifiedName = new MockUtil().getMockName(invocation.getMock()) + "." + invocation.getMethod().getName(); String invocationString = qualifiedName + matchersPrinter.getArgumentsLine(matchers, this); if (isMultiline() || (!matchers.isEmpty() && invocationString.length() > MAX_LINE_LENGTH)) { return qualifiedName + matchersPrinter.getArgumentsBlock(matchers, this); } else { return invocationString; } }
/** * Finds all unused stubs for given mocks * * @param mocks */ public List<Invocation> find(List<?> mocks) { List<Invocation> unused = new LinkedList<Invocation>(); for (Object mock : mocks) { InternalMockHandler<Object> handler = new MockUtil().getMockHandler(mock); List<StubbedInvocationMatcher> fromSingleMock = handler.getInvocationContainer().getStubbedInvocations(); for(StubbedInvocationMatcher s : fromSingleMock) { if (!s.wasUsed()) { unused.add(s.getInvocation()); } } } return unused; } }
if (new MockUtil().isMock(instance)) {
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; }
@Override protected boolean processInjection(Field field, Object fieldOwner, Set<Object> mockCandidates) { FieldReader fieldReader = new FieldReader(fieldOwner, field); // TODO refoctor : code duplicated in SpyAnnotationEngine if(!fieldReader.isNull() && field.isAnnotationPresent(Spy.class)) { try { Object instance = fieldReader.read(); if (new MockUtil().isMock(instance)) { // A. instance has been spied earlier // B. protect against multiple use of MockitoAnnotations.initMocks() Mockito.reset(instance); } else { new FieldSetter(fieldOwner, field).set( Mockito.mock(instance.getClass(), withSettings() .spiedInstance(instance) .defaultAnswer(Mockito.CALLS_REAL_METHODS) .name(field.getName())) ); } } catch (Exception e) { throw new MockitoException("Problems initiating spied field " + field.getName(), e); } } return false; } }
public Object answer(InvocationOnMock invocation) { if (methodsGuru.isToString(invocation.getMethod())) { Object mock = invocation.getMock(); MockName name = new MockUtil().getMockName(mock); if (name.isDefault()) { return "Mock for " + ClassNameFinder.classNameForMock(mock) + ", hashCode: " + mock.hashCode(); } else { return name.toString(); } } else if (methodsGuru.isCompareToMethod(invocation.getMethod())) { //see issue 184. //mocks by default should not return 0 for compareTo because they are not the same. Hence we return 1 (anything but 0 is good). //Only for compareTo() method by the Comparable interface return 1; } Class<?> returnType = invocation.getMethod().getReturnType(); return returnValueFor(returnType); }
MockUtil mockUtil = new MockUtil(); MockName mockName = mockUtil.getMockName(mockitoMock); String mockedType = mockUtil.getMockSettings(mockitoMock).getTypeToMock().getCanonicalName();
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); }
@Override public void describeMismatch(Object item, final Description description) { description.appendText("had these: "); for (Invocation invocation : new MockUtil().getMockHandler(item).getInvocationContainer().getInvocations()) description.appendText(invocation.toString()); }
when(mockSDKInfo.getVersion()).thenReturn(sdkVersion); final MockUtil mockUtil = new MockUtil(); if (mockUtil.isMock(mockSystem)) { when(mockSystem.getPreferences()).thenReturn(mockPreferences);
@Before public void setup() { WXEnvironment.sApplication = RuntimeEnvironment.application; WXSDKManager wxsdkManager = WXSDKManager.getInstance(); if (!new MockUtil().isSpy(wxsdkManager)) { WXSDKManager spy = Mockito.spy(wxsdkManager); WXSDKManagerTest.setInstance(spy); Mockito.when(spy.getIWXHttpAdapter()).thenReturn(new IWXHttpAdapter() { @Override public void sendRequest(WXRequest request, OnHttpListener listener) { //do nothing. } }); } adapter = new DefaultUriAdapter(); instance = WXSDKInstanceTest.createInstance(); }