/** * Joins Strings with EOL character * * @param start the starting String * @param lines collection to join */ public static String join(String start, Collection<?> lines) { return join(start, "", lines); }
public static String decamelizeMatcher(String className) { if (className.length() == 0) { return "<custom argument matcher>"; } String decamelized = decamelizeClassName(className); if (decamelized.length() == 0) { return "<" + className + ">"; } return "<" + decamelized + ">"; }
@Override public String toString() { return removeFirstLine(super.toString()); } }
/** * Attempts to provide more descriptive toString() for given matcher. * Searches matcher class hierarchy for toString() method. If it is found it will be used. * If no toString() is defined for the matcher hierarchy, * uses decamelized class name instead of default Object.toString(). * This way we can promote meaningful names for custom matchers. * * @param matcher * @return */ static String toString(ArgumentMatcher<?> matcher) { Class<?> cls = matcher.getClass(); while(cls != Object.class) { Method[] methods = cls.getDeclaredMethods(); for (Method m : methods) { if(isToStringMethod(m)) { return matcher.toString(); } } cls = cls.getSuperclass(); } return decamelizeMatcher(matcher.getClass().getSimpleName()); }
/** * Joins Strings with line break character. It adds line break in front, too. * This makes it something like 'format' no really 'join'. */ public static String join(Object ... linesToBreak) { return join("\n", asList(linesToBreak)); }
@Override public String toString() { return removeFirstLine(super.toString()); } }
public static MockitoException checkedExceptionInvalid(Throwable t) { return new MockitoException(join( "Checked exception is invalid for this method!", "Invalid: " + t )); }
@Override public String toString() { return removeFirstLine(super.toString()); } }
public static MockitoException notAMockPassedToWhenMethod() { return new NotAMockException(join( "Argument passed to when() is not a mock!", "Example of correct stubbing:", " doThrow(new RuntimeException()).when(mock).someMethod();" )); }
public static MockitoException mocksHaveToBePassedToVerifyNoMoreInteractions() { return new MockitoException(join( "Method requires argument(s)!", "Pass mocks that should be verified, e.g:", " verifyNoMoreInteractions(mockOne, mockTwo);", " verifyZeroInteractions(mockOne, mockTwo);", "" )); }
public static MockitoException notAMockPassedToVerifyNoMoreInteractions() { return new NotAMockException(join( "Argument(s) passed is not a mock!", "Examples of correct verifications:", " verifyNoMoreInteractions(mockOne, mockTwo);", " verifyZeroInteractions(mockOne, mockTwo);", "" )); }
public static MockitoException mockedTypeIsInconsistentWithSpiedInstanceType(Class<?> mockedType, Object spiedInstance) { return 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 static void redundantMockitoListener(String listenerType) { throw new RedundantListenerException(join( "Problems adding Mockito listener.", "Listener of type '" + listenerType + "' has already been added and not removed.", "It indicates that previous listener was not removed according to the API.", "When you add a listener, don't forget to remove the listener afterwards:", " Mockito.framework().removeListener(myListener);", "For more information, see the javadoc for RedundantListenerException class.")); }
private static Object invoke(Method method, Object target, Object... args) { try { return method.invoke(target, args); } catch (Exception e) { throw new MockitoException(join("Could not invoke " + method + " using reflection", "", "Mockito attempted to interact with the Java module system but an unexpected method behavior was encountered"), e); } } }
public static MockitoException nullPassedToVerify() { return new NullInsteadOfMockException(join( "Argument passed to verify() should be a mock but is null!", "Examples of correct verifications:", " verify(mock).someMethod();", " verify(mock, times(10)).someMethod();", " verify(mock, atLeastOnce()).someMethod();", " not: verify(mock.someMethod());", "Also, if you use @Mock annotation don't miss initMocks()" )); }
public static MockitoException nullPassedWhenCreatingInOrder() { return new NullInsteadOfMockException(join( "Argument(s) passed is null!", "Pass mocks that require verification in order.", "For example:", " InOrder inOrder = inOrder(mockOne, mockTwo);" )); }
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.", "" )); }