public void nullPassedWhenCreatingInOrder() { throw new NullInsteadOfMockException(join( "Argument(s) passed is null!", "Pass mocks that require verification in order.", "For example:", " InOrder inOrder = inOrder(mockOne, mockTwo);" )); }
private String createWantedButNotInvokedMessage(DescribedInvocation wanted) { return join( "Wanted but not invoked:", wanted.toString(), new LocationImpl(), "" ); }
public void invocationListenerThrewException(InvocationListener listener, Throwable listenerThrowable) { throw new MockitoException(StringJoiner.join( "The invocation listener with type " + listener.getClass().getName(), "threw an exception : " + listenerThrowable.getClass().getName() + listenerThrowable.getMessage()), listenerThrowable); }
public void notAMockPassedToWhenMethod() { throw new NotAMockException(join( "Argument passed to when() is not a mock!", "Example of correct stubbing:", " doThrow(new RuntimeException()).when(mock).someMethod();" )); }
public void nullPassedToVerifyNoMoreInteractions() { throw new NullInsteadOfMockException(join( "Argument(s) passed is null!", "Examples of correct verifications:", " verifyNoMoreInteractions(mockOne, mockTwo);", " verifyZeroInteractions(mockOne, mockTwo);" )); }
public void notAMockPassedWhenCreatingInOrder() { throw new NotAMockException(join( "Argument(s) passed is not a mock!", "Pass mocks that require verification in order.", "For example:", " InOrder inOrder = inOrder(mockOne, mockTwo);" )); }
public void mocksHaveToBePassedWhenCreatingInOrder() { throw new MockitoException(join( "Method requires argument(s)!", "Pass mocks that require verification in order.", "For example:", " InOrder inOrder = inOrder(mockOne, mockTwo);" )); }
public void inOrderRequiresFamiliarMock() { throw new MockitoException(join( "InOrder can only verify mocks that were passed in during creation of InOrder.", "For example:", " InOrder inOrder = inOrder(mockOne);", " inOrder.verify(mockOne).doStuff();" )); }
public void extraInterfacesDoesNotAcceptNullParameters() { throw new MockitoException(join( "extraInterfaces() does not accept null parameters." )); }
public void extraInterfacesAcceptsOnlyInterfaces(Class<?> wrongType) { throw new MockitoException(join( "extraInterfaces() accepts only interfaces.", "You passed following type: " + wrongType.getSimpleName() + " which is not an interface." )); }
public void cannotInitializeForInjectMocksAnnotation(String fieldName, Exception details) { throw new MockitoException(join("Cannot instantiate @InjectMocks field named '" + fieldName + "'.", "You haven't provided the instance at field declaration so I tried to construct the instance.", "However, I failed because: " + details.getMessage(), "Examples of correct usage of @InjectMocks:", " @InjectMocks Service service = new Service();", " @InjectMocks Service service;", " //also, don't forget about MockitoAnnotations.initMocks();", " //and... don't forget about some @Mocks for injection :)", ""), details); }
public void invalidArgumentRangeAtIdentityAnswerCreationTime() { throw new MockitoException(join("Invalid argument index.", "The index need to be a positive number that indicates the position of the argument to return.", "However it is possible to use the -1 value to indicates that the last argument should be", "returned.")); }
public void cannotMockFinalClass(Class<?> clazz) { throw new MockitoException(join( "Cannot mock/spy " + clazz.toString(), "Mockito cannot mock/spy following:", " - final classes", " - anonymous classes", " - primitive types" )); }
public void extraInterfacesCannotContainMockedType(Class<?> wrongType) { throw new MockitoException(join( "extraInterfaces() does not accept the same type as the mocked type.", "You mocked following type: " + wrongType.getSimpleName(), "and you passed the same very interface to the extraInterfaces()" )); }
public void mockedTypeIsInconsistentWithSpiedInstanceType(Class<?> mockedType, Object spiedInstance) { throw new MockitoException(join( "Mocked type must be the same as the type of your spied instance.", "Mocked type must be: " + spiedInstance.getClass().getSimpleName() + ", but is: " + mockedType.getSimpleName(), " //correct spying:", " spy = mock( ->ArrayList.class<- , withSettings().spiedInstance( ->new ArrayList()<- );", " //incorrect - types don't match:", " spy = mock( ->List.class<- , withSettings().spiedInstance( ->new ArrayList()<- );" )); }
public void cannotCallRealMethodOnInterface() { throw new MockitoException(join( "Cannot call real method on java interface. Interface does not have any implementation!", "Calling real methods is only possible when mocking concrete classes.", " //correct example:", " when(mockOfConcreteClass.doStuff()).thenCallRealMethod();" )); }
public void cannotInitializeForSpyAnnotation(String fieldName, Exception details) { throw new MockitoException(join("Cannot instantiate a @Spy for '" + fieldName + "' field.", "You haven't provided the instance for spying at field declaration so I tried to construct the instance.", "However, I failed because: " + details.getMessage(), "Examples of correct usage of @Spy:", " @Spy List mock = new LinkedList();", " @Spy Foo foo; //only if Foo has parameterless constructor", " //also, don't forget about MockitoAnnotations.initMocks();", ""), details); }
public void mockedTypeIsInconsistentWithDelegatedInstanceType(Class mockedType, Object delegatedInstance) { throw new MockitoException(join( "Mocked type must be the same as the type of your delegated instance.", "Mocked type must be: " + delegatedInstance.getClass().getSimpleName() + ", but is: " + mockedType.getSimpleName(), " //correct delegate:", " spy = mock( ->List.class<- , withSettings().delegatedInstance( ->new ArrayList()<- );", " //incorrect - types don't match:", " spy = mock( ->List.class<- , withSettings().delegatedInstance( ->new HashSet()<- );" )); }
public void cannotStubWithNullThrowable() { throw new MockitoException(join( "Cannot stub with null throwable!" )); }
public void notAMockPassedToVerify(Class type) { throw new NotAMockException(join( "Argument passed to verify() is of type " + type.getSimpleName() + " and is not a mock!", "Make sure you place the parenthesis correctly!", "See the examples of correct verifications:", " verify(mock).someMethod();", " verify(mock, times(10)).someMethod();", " verify(mock, atLeastOnce()).someMethod();" )); }