private static Constructor<?> noArgConstructorOf(Class<?> type) { Constructor<?> constructor; try { constructor = type.getDeclaredConstructor(); } catch (NoSuchMethodException e) { throw new MockitoException("Please ensure that the type '" + type.getSimpleName() + "' has a no-arg constructor."); } return constructor; }
private void checkNotEnum(Field field) { if(field.getType().isEnum()) { throw new MockitoException("the type '" + field.getType().getSimpleName() + "' is an enum."); } }
private void checkNotLocal(Field field) { if(field.getType().isLocalClass()) { throw new MockitoException("the type '" + field.getType().getSimpleName() + "' is a local class."); } }
private void checkNotInner(Field field) { Class<?> type = field.getType(); if(type.isMemberClass() && !isStatic(type.getModifiers())) { throw new MockitoException("the type '" + type.getSimpleName() + "' is an inner non static class."); } }
private <T> void checkSupportedCombination(boolean subclassingRequired, MockFeatures<T> features) { if (subclassingRequired && !features.mockedType.isArray() && !features.mockedType.isPrimitive() && Modifier.isFinal(features.mockedType.getModifiers())) { throw new MockitoException("Unsupported settings with this type '" + features.mockedType.getName() + "'"); } }
public static MockitoException cannotMockClass(Class<?> clazz, String reason) { return new MockitoException(join( "Cannot mock/spy " + clazz.toString(), "Mockito cannot mock/spy because :", " - " + reason )); }
public static MockitoException onlyVoidMethodsCanBeSetToDoNothing() { return new MockitoException(join( "Only void methods can doNothing()!", "Example of correct use of doNothing():", " doNothing().", " doThrow(new RuntimeException())", " .when(mock).someVoidMethod();", "Above means:", "someVoidMethod() does nothing the 1st time but throws an exception the 2nd time is called" )); }
public static MockitoException cannotInitializeForSpyAnnotation(String fieldName, Exception details) { return 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 static MockitoException serializableWontWorkForObjectsThatDontImplementSerializable(Class<?> classToMock) { return new MockitoException(join( "You are using the setting 'withSettings().serializable()' however the type you are trying to mock '" + classToMock.getSimpleName() + "'", "do not implement Serializable AND do not have a no-arg constructor.", "This combination is requested, otherwise you will get an 'java.io.InvalidClassException' when the mock will be serialized", "", "Also note that as requested by the Java serialization specification, the whole hierarchy need to implements Serializable,", "i.e. the top-most superclass has to implements Serializable.", "" )); }
public static MockitoException extraInterfacesDoesNotAcceptNullParameters() { return new MockitoException(join( "extraInterfaces() does not accept null parameters." )); }
public static MockitoException cannotInitializeForInjectMocksAnnotation(String fieldName, String causeMessage) { return new MockitoException(join("Cannot instantiate @InjectMocks field named '" + fieldName + "'! Cause: "+causeMessage, "You haven't provided the instance at field declaration so I tried to construct the instance.", "Examples of correct usage of @InjectMocks:", " @InjectMocks Service service = new Service();", " @InjectMocks Service service;", " //and... don't forget about some @Mocks for injection :)", "")); }
public static MockitoException invocationListenerThrewException(InvocationListener listener, Throwable listenerThrowable) { return new MockitoException(join( "The invocation listener with type " + listener.getClass().getName(), "threw an exception : " + listenerThrowable.getClass().getName() + listenerThrowable.getMessage()), listenerThrowable); }
public static MockitoException mockedTypeIsInconsistentWithDelegatedInstanceType(Class<?> mockedType, Object delegatedInstance) { return 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 static MockitoException cannotStubWithNullThrowable() { return new MockitoException(join( "Cannot stub with null throwable!" )); }
public static MockitoException inOrderRequiresFamiliarMock() { return 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 static MockitoException extraInterfacesCannotContainMockedType(Class<?> wrongType) { return 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 static MockitoException delegatedMethodHasWrongReturnType(Method mockMethod, Method delegateMethod, Object mock, Object delegate) { return new MockitoException(join( "Methods called on delegated instance must have compatible return types with the mock.", "When calling: " + mockMethod + " on mock: " + MockUtil.getMockName(mock), "return type should be: " + mockMethod.getReturnType().getSimpleName() + ", but was: " + delegateMethod.getReturnType().getSimpleName(), "Check that the instance passed to delegatesTo() is of the correct type or contains compatible methods", "(delegate instance had type: " + delegate.getClass().getSimpleName() + ")" )); }
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() + ")" )); }
private static SubclassLoader tryLookup() { try { Class<?> methodHandles = Class.forName("java.lang.invoke.MethodHandles"); Object lookup = methodHandles.getMethod("lookup").invoke(null); Method privateLookupIn = methodHandles.getMethod("privateLookupIn", Class.class, Class.forName("java.lang.invoke.MethodHandles$Lookup")); Object codegenLookup = privateLookupIn.invoke(null, InjectionBase.class, lookup); return new WithLookup(lookup, codegenLookup, privateLookupIn); } catch (Exception exception) { throw new MockitoException(join(ERROR_MESSAGE, "", Platform.describe()), exception); } }
public static MockitoException cannotInjectDependency(Field field, Object matchingMock, Exception details) { return new MockitoException(join( "Mockito couldn't inject mock dependency '" + MockUtil.getMockName(matchingMock) + "' on field ", "'" + field + "'", "whose type '" + field.getDeclaringClass().getCanonicalName() + "' was annotated by @InjectMocks in your test.", "Also I failed because: " + exceptionCauseMessageIfAvailable(details), "" ), details); }