/** * 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 MockitoException checkedExceptionInvalid(Throwable t) { return new MockitoException(join( "Checked exception is invalid for this method!", "Invalid: " + t )); }
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 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 )); }
private InstantiationException multipleMatchingConstructors(Class<?> cls, List<Constructor<?>> constructors) { return new InstantiationException(join("Unable to create instance of '" + cls.getSimpleName() + "'.", "Multiple constructors could be matched to arguments of types " + constructorArgTypes() + ":", join("", " - ", constructors), "If you believe that Mockito could do a better job deciding on which constructor to use, please let us know.", "Ticket 685 contains the discussion and a workaround for ambiguous constructors using inner class.", "See https://github.com/mockito/mockito/issues/685" ), null); }
private static Object locationsOf(Collection<LocalizedMatcher> matchers) { List<String> description = new ArrayList<String>(); for (LocalizedMatcher matcher : matchers) description.add(matcher.getLocation().toString()); return join(description.toArray()); }
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 InstantiationException paramsException(Class<?> cls, Exception e) { return new InstantiationException(join( "Unable to create instance of '" + cls.getSimpleName() + "'.", "Please ensure the target class has " + constructorArgsString() + " and executes cleanly.") , e); }
public static AssertionError argumentsAreDifferent(String wanted, String actual, Location actualLocation) { String message = join("Argument(s) are different! Wanted:", wanted, new LocationImpl(), "Actual invocation has different arguments:", actual, actualLocation, "" ); return ExceptionFactory.createArgumentsAreDifferentException(message, wanted, actual); }
private static String createWantedButNotInvokedMessage(DescribedInvocation wanted) { return join( "Wanted but not invoked:", wanted.toString(), new LocationImpl(), "" ); }
public static MockitoException incorrectUseOfApi() { return new MockitoException(join( "Incorrect use of API detected here:", new LocationImpl(), "", "You probably stored a reference to OngoingStubbing returned by when() and called stubbing methods like thenReturn() on this reference more than once.", "Examples of correct usage:", " when(mock.isOk()).thenReturn(true).thenReturn(false).thenThrow(exception);", " when(mock.isOk()).thenReturn(true, false).thenThrow(exception);", "" )); }
public static MockitoException reportNoSubMatchersFound(String additionalMatcherName) { return new InvalidUseOfMatchersException(join( "No matchers found for additional matcher " + additionalMatcherName, new LocationImpl(), "" )); }
public InlineByteBuddyMockMaker() { if (INITIALIZATION_ERROR != null) { throw new MockitoInitializationException(join( "Could not initialize inline Byte Buddy mock maker. (This mock maker is not supported on Android.)", ToolProvider.getSystemJavaCompiler() == null ? "Are you running a JRE instead of a JDK? The inline mock maker needs to be run on a JDK.\n" : "", Platform.describe()), INITIALIZATION_ERROR); } bytecodeGenerator = new TypeCachingBytecodeGenerator(new InlineBytecodeGenerator(INSTRUMENTATION, mocks), true); }
private static String createTooLittleInvocationsMessage(org.mockito.internal.reporting.Discrepancy discrepancy, DescribedInvocation wanted, List<Location> locations) { return join( wanted.toString(), "Wanted " + discrepancy.getPluralizedWantedCount() + (discrepancy.getWantedCount() == 0 ? "." : ":"), new LocationImpl(), "But was " + discrepancy.getPluralizedActualCount() + (discrepancy.getActualCount() == 0 ? "." : ":"), createAllLocationsMessage(locations) ); }