private void invokeSubstitute(Object... arguments) throws Exception { try { invocationControl.getSubstitute().performSubstitutionLogic(arguments); } catch (MockitoAssertionError e) { InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } }
public void verifyNoMoreInteractions() { try { final MockHandler mockHandler = methodInterceptorFilter.getHandler(); if (mockHandler instanceof MockHandler) { InvocationContainer invocationContainer = Whitebox.invokeMethod(mockHandler, "getInvocationContainer"); VerificationDataImpl data = new VerificationDataImpl(invocationContainer, null); VerificationModeFactory.noMoreInteractions().verify(data); } else { throw new RuntimeException( "Cannot perform verifyNoMoreInteractions because of unknown mockhandler type " + mockHandler.getClass()); } } catch (MockitoAssertionError e) { InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e); throw e; } catch (Exception e) { throw new RuntimeException("PowerMock internal error",e); } }
private Object performIntercept(MethodInterceptorFilter invocationHandler, final Object interceptionObject, final Method method, Object[] arguments) throws Throwable { MockHandler mockHandler = invocationHandler.getHandler(); final CleanTraceRealMethod cglibProxyRealMethod = new CleanTraceRealMethod(new MockedRealMethod(method, interceptionObject)); Invocation invocation = new InvocationImpl( interceptionObject, new DelegatingMethod(method), arguments, SequenceNumber.next(), cglibProxyRealMethod) { private static final long serialVersionUID = -3679957412502758558L; @Override public String toString() { return new ToStringGenerator().generate(getMock(), getMethod(), getArguments()); } }; try { return replaceMatchersBinderIfNeeded(mockHandler).handle(invocation); } catch (NotAMockException e) { if(invocation.getMock().getClass().getName().startsWith("java.") && MockRepository.getInstanceMethodInvocationControl(invocation.getMock()) != null) { return invocation.callRealMethod(); } else { throw e; } } catch (MockitoAssertionError e) { InvocationControlAssertionError.updateErrorMessageForMethodInvocation(e); throw e; } }
private static String changeMessageContent(String message) { /* * Temp fix: Remove powermock internal "at locations" (points to which * line the expectation went wrong in Mockito). We should try to find * the real ones instead */ StringBuilder builder = removeFailureLocations(message); // Remove "Undesired invocation:" removeText(builder, UNDESIRED_INVOCATION_TEXT); removeAndReplaceText(builder, HERE_TEXT, ' '); removeAndReplaceText(builder, COLON_NEWLINE, ' '); return builder.toString().trim(); }
public static void updateErrorMessageForMethodInvocation(AssertionError errorToUpdate) { /* * We failed to verify the new substitution mock. This happens when, for * example, the user has done something like * whenNew(MyClass.class).thenReturn(myMock).times(3) when in fact an * instance of MyClass has been created less or more times than 3. */ Whitebox.setInternalState(errorToUpdate, "detailMessage", "\n" + changeMessageContent(errorToUpdate.getMessage())); }
private static void removeAndReplaceText(StringBuilder builder, String text, char appender) { int currentTextIndex = builder.indexOf(text); int previousTextIndex = 0; boolean isSingleConcat = true; while (currentTextIndex > 0) { previousTextIndex = currentTextIndex; builder.delete(currentTextIndex, currentTextIndex + text.length()); currentTextIndex = builder.indexOf(text); if (isLastFinding(currentTextIndex) && !isSingleConcat) { builder.replace(builder.length(), builder.length(), "."); } else { builder.replace(previousTextIndex, previousTextIndex + 1, String.valueOf( builder.charAt(previousTextIndex)).toLowerCase()); builder.insert(previousTextIndex, String.valueOf(appender)); currentTextIndex++; isSingleConcat = false; } } }
private static String changeMessageContent(String message) { /* * Temp fix: Remove powermock internal "at locations" (points to which * line the expectation went wrong in Mockito). We should try to find * the real ones instead */ StringBuilder builder = removeFailureLocations(message); // Remove "Undesired invocation:" removeText(builder, UNDESIRED_INVOCATION_TEXT); removeAndReplaceText(builder, HERE_TEXT, ' '); removeAndReplaceText(builder, COLON_NEWLINE, ' '); final String finalMessage = builder.toString().trim(); return finalMessage; }
public static void updateErrorMessageForMethodInvocation(AssertionError errorToUpdate) { /* * We failed to verify the new substitution mock. This happens when, for * example, the user has done something like * whenNew(MyClass.class).thenReturn(myMock).times(3) when in fact an * instance of MyClass has been created less or more times than 3. */ Whitebox.setInternalState(errorToUpdate, "\n" + changeMessageContent(errorToUpdate.getMessage())); }
private static void removeAndReplaceText(StringBuilder builder, String text, char appender) { int currentTextIndex = builder.indexOf(text); int previousTextIndex; boolean isSingleConcat = true; while (currentTextIndex > 0) { previousTextIndex = currentTextIndex; builder.delete(currentTextIndex, currentTextIndex + text.length()); currentTextIndex = builder.indexOf(text); final int length = builder.length(); if (isLastFinding(currentTextIndex) && !isSingleConcat) { final int start = builder.charAt(length - 1) == '\n' ? length - 1 : length; builder.replace(start, length, "."); } else { final int end = previousTextIndex < length ? previousTextIndex + 1 : length; builder.replace( previousTextIndex, end, String.valueOf(builder.charAt(previousTextIndex)).toLowerCase() ); builder.insert(previousTextIndex, String.valueOf(appender)); currentTextIndex++; isSingleConcat = false; } } }
private void invokeSubstitute(Object... arguments) throws Exception { try { invocationControl.getSubstitute().performSubstitutionLogic(arguments); } catch (MockitoAssertionError e) { InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } }
public void verifyNoMoreInteractions() { try { Mockito.verifyNoMoreInteractions(substitute); } catch (MockitoAssertionError e) { InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e); throw e; } } }
public static void throwAssertionErrorForNewSubstitutionFailure(AssertionError oldError, Class<?> type) { /* * We failed to verify the new substitution mock. This happens when, for * example, the user has done something like * whenNew(MyClass.class).thenReturn(myMock).times(3) when in fact an * instance of MyClass has been created less or more times than 3. */ final String newSubsitutionClassName = InvocationSubstitute.class.getSimpleName(); final String newSubsitutionClassNameInMockito = newSubsitutionClassName.substring(0, 1).toLowerCase() + newSubsitutionClassName.substring(1); String message = oldError.getMessage(); final String newSubsitutionMethodName = InvocationSubstitute.class.getDeclaredMethods()[0].getName(); message = message.replaceAll(newSubsitutionClassNameInMockito + "." + newSubsitutionMethodName, Matcher .quoteReplacement(type.getName())); message = message.replaceAll("method", "constructor"); throw new AssertionError(changeMessageContent(message)); }
Object performIntercept(final Object mock, final Method method, Object[] arguments) throws Throwable { Invocation invocation = createInvocation(mock, method, arguments); try { return getMockHandler().handle(invocation); } catch (NotAMockException e) { if (invocation.getMock() .getClass() .getName() .startsWith("java.") && MockRepository.getInstanceMethodInvocationControl(invocation.getMock()) != null) { return invocation.callRealMethod(); } else { throw e; } } catch (MockitoAssertionError e) { InvocationControlAssertionError.updateErrorMessageForMethodInvocation(e); throw e; } }
@Override public Object invoke(Class<?> type, Object[] args, Class<?>[] sig) throws Exception { Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig); if (constructor.isVarArgs()) { Object varArgs = args[args.length - 1]; final int varArgsLength = Array.getLength(varArgs); Object[] oldArgs = args; args = new Object[args.length + varArgsLength - 1]; System.arraycopy(oldArgs, 0, args, 0, oldArgs.length - 1); for (int i = oldArgs.length - 1, j=0; i < args.length; i++, j++) { args[i] = Array.get(varArgs, j); } } try { return substitute.performSubstitutionLogic(args); } catch (MockitoAssertionError e) { InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } // Won't happen return null; }
public void verifyNoMoreInteractions() { try { Mockito.verifyNoMoreInteractions(substitute); } catch (MockitoAssertionError e) { InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e); throw e; } } }
public static void throwAssertionErrorForNewSubstitutionFailure(AssertionError oldError, Class<?> type) { /* * We failed to verify the new substitution mock. This happens when, for * example, the user has done something like * whenNew(MyClass.class).thenReturn(myMock).times(3) when in fact an * instance of MyClass has been created less or more times than 3. */ final String newSubsitutionClassName = InvocationSubstitute.class.getSimpleName(); final String newSubsitutionClassNameInMockito = newSubsitutionClassName.substring(0, 1).toLowerCase() + newSubsitutionClassName.substring(1); String message = oldError.getMessage(); final String newSubsitutionMethodName = InvocationSubstitute.class.getDeclaredMethods()[0].getName(); message = message.replaceAll(newSubsitutionClassNameInMockito + "." + newSubsitutionMethodName, Matcher .quoteReplacement(type.getName())); message = message.replaceAll("method", "constructor"); throw new AssertionError(changeMessageContent(message)); }
@Override public Object invoke(Class<?> type, Object[] args, Class<?>[] sig) throws Exception { Constructor<?> constructor = WhiteboxImpl.getConstructor(type, sig); if (constructor.isVarArgs()) { Object varArgs = args[args.length - 1]; final int varArgsLength = Array.getLength(varArgs); Object[] oldArgs = args; args = new Object[args.length + varArgsLength - 1]; System.arraycopy(oldArgs, 0, args, 0, oldArgs.length - 1); for (int i = oldArgs.length - 1, j=0; i < args.length; i++, j++) { args[i] = Array.get(varArgs, j); } } try { return substitute.performSubstitutionLogic(args); } catch (MockitoAssertionError e) { InvocationControlAssertionError.throwAssertionErrorForNewSubstitutionFailure(e, type); } // Won't happen return null; }
public void verifyNoMoreInteractions() { try { Mockito.verifyNoMoreInteractions(getMockHandlerAdaptor().getMock()); } catch (MockitoAssertionError e) { //TODO replace this dirty hack InvocationControlAssertionError.updateErrorMessageForVerifyNoMoreInteractions(e); throw e; } catch (Exception e) { throw new RuntimeException("PowerMock internal error", e); } }