private static AssertionError createLeakError(DataBuffer delegate) { String message = String.format("DataBuffer leak detected: {%s} has not been released.%n" + "Stack trace of buffer allocation statement follows:", delegate); AssertionError result = new AssertionError(message); // remove first four irrelevant stack trace elements StackTraceElement[] oldTrace = result.getStackTrace(); StackTraceElement[] newTrace = new StackTraceElement[oldTrace.length - 4]; System.arraycopy(oldTrace, 4, newTrace, 0, oldTrace.length - 4); result.setStackTrace(newTrace); return result; }
/** * Creates a copy of the given assertion error with the custom failure message prepended. * @param error The assertion error to copy * @param message The custom message to prepend * @since 2.1.0 */ public MockitoAssertionError(MockitoAssertionError error, String message) { super(message + "\n" + error.getMessage()); super.setStackTrace(error.getStackTrace()); unfilteredStackTrace = error.getUnfilteredStackTrace(); }
static void fail(@Nullable Object actualValue, Matcher<?> matcher) { Description description = new StringDescription() .appendText("\nExpected: ") .appendDescriptionOf(matcher) .appendText("\n but: "); matcher.describeMismatch(actualValue, description); AssertionError assertionError = new AssertionError(description.toString()); assertionError.setStackTrace(ObjectChecker.trimStackTrace(assertionError.getStackTrace())); throw assertionError; }
private void removeCustomAssertRelatedElementsFromStackTraceIfNeeded(AssertionError assertionError) { if (!Failures.instance().isRemoveAssertJRelatedElementsFromStackTrace()) return; if (isAssertjAssertClass()) return; StackTraceElement[] newStackTrace = Arrays.stream(assertionError.getStackTrace()) .filter(element -> !isElementOfCustomAssert(element)) .toArray(StackTraceElement[]::new); assertionError.setStackTrace(newStackTrace); }
private void removeCustomAssertRelatedElementsFromStackTraceIfNeeded(AssertionError assertionError) { if (!Failures.instance().isRemoveAssertJRelatedElementsFromStackTrace()) return; if (isAssertjAssertClass()) return; StackTraceElement[] newStackTrace = Arrays.stream(assertionError.getStackTrace()) .filter(element -> !isElementOfCustomAssert(element)) .toArray(StackTraceElement[]::new); assertionError.setStackTrace(newStackTrace); }
@Override public T apply( GraphDatabaseService graphDb ) { try { return perform( graphDb ); } catch ( AssertionError e ) { AssertionError error = new AssertionError( message + ": " + e.getMessage() ); error.setStackTrace( e.getStackTrace() ); throw error; } }
StackTraceElement[] newStackTrace = new StackTraceElement[stackTrace.length - framesToPop]; System.arraycopy(stackTrace, framesToPop, newStackTrace, 0, newStackTrace.length); e.setStackTrace(newStackTrace); throw e;
StackTraceElement[] newStackTrace = new StackTraceElement[stackTrace.length - framesToPop]; System.arraycopy(stackTrace, framesToPop, newStackTrace, 0, newStackTrace.length); e.setStackTrace(newStackTrace); throw e;
originalFailure); e.setStackTrace(smallerFailure.getStackTrace()); return e;
public AssertionError wrap(String message) { String outputMessage = (this.message == null || this.message.isEmpty()) ? message : (this.message + ": " + message); AssertionError res = new AssertionError(outputMessage); res.setStackTrace(creationStackTrace); return res; }
public AssertionError wrap(Throwable t) { AssertionError res = new AssertionError( message.isEmpty() ? t.getMessage() : (message + ": " + t.getMessage()), t); res.setStackTrace(creationStackTrace); return res; }
@Override public void evaluate() throws Throwable { try { base.evaluate(); } catch(Throwable e) { String message = e.getMessage() == null ? "" : e.getMessage(); AssertionError error = new AssertionError(format("%s (Seed was %sL.)", message, seed)); error.setStackTrace(e.getStackTrace()); throw error; } } };
public static void fail(@Nullable String message) { final AssertionError error = message != null ? new AssertionError(message) : new AssertionError(); error.setStackTrace(getCleanStackTrace()); throw error; }
private AssertionError generateError(final List<AssertionError> assertionErrors) { AssertionError error = new AssertionError(joinMessages(assertionErrors)); List<StackTraceElement> allElements = Lists.newArrayList(); for (AssertionError assertionError : assertionErrors) { allElements.addAll(Arrays.asList(assertionError.getStackTrace())); } error.setStackTrace(allElements.toArray(new StackTraceElement[allElements.size()])); return error; }
/** * Assert that all connections are closed. */ public static void assertConnectionsClosed() { int count = INSTANCE.getOpenConnections(); if (count > 0) { AssertionError error = new AssertionError(count + " connection(s) not closed"); error.setStackTrace(openConnections.iterator().next().getCaller()); throw error; } }
public void assertNoMoreInvocations(StackTraceElement[] assertedAt) { List<ObservedInvocation> unexpectedInvocations = new ArrayList<ObservedInvocation>(); for (int i = 0; i < observedInvocations.size(); i++) { ObservedInvocation observedInvocation = observedInvocations.get(i); VerificationStatus invocationVerificationStatus = invocationVerificationStatuses.get(i); if (observedInvocation.getMockBehavior() == null && invocationVerificationStatus == UNVERIFIED) { unexpectedInvocations.add(observedInvocation); } } if (unexpectedInvocations.size() != 0) { AssertionError assertionError = new AssertionError(getNoMoreInvocationsErrorMessage(unexpectedInvocations, assertedAt[0])); assertionError.setStackTrace(assertedAt); throw assertionError; } }
private AssertionError newAssertionError(RowResults results) { AssertionError error = new AssertionError(createMessage(results)); error.setStackTrace(results.getStackTrace()); return error; }
public void assertNotInvoked(BehaviorDefiningInvocation assertInvocation) { for (int i = 0; i < observedInvocations.size(); i++) { ObservedInvocation observedInvocation = observedInvocations.get(i); VerificationStatus invocationVerificationStatus = invocationVerificationStatuses.get(i); if (invocationVerificationStatus == UNVERIFIED && assertInvocation.matches(observedInvocation) != -1) { AssertionError assertionError = new AssertionError(getAssertNotInvokedErrorMessage(assertInvocation, observedInvocation, assertInvocation.getInvokedAtTrace())); assertionError.setStackTrace(assertInvocation.getInvokedAtTrace()); throw assertionError; } } }
public void assertInvoked(BehaviorDefiningInvocation assertInvocation) { for (int i = 0; i < observedInvocations.size(); i++) { ObservedInvocation observedInvocation = observedInvocations.get(i); VerificationStatus invocationVerificationStatus = invocationVerificationStatuses.get(i); if (invocationVerificationStatus == UNVERIFIED && assertInvocation.matches(observedInvocation) != -1) { // Found a match that's not verified yet. Mark as verified and proceed. invocationVerificationStatuses.set(i, VERIFIED); return; } } AssertionError assertionError = new AssertionError(getAssertInvokedErrorMessage(assertInvocation, assertInvocation.getInvokedAt())); assertionError.setStackTrace(assertInvocation.getInvokedAtTrace()); throw assertionError; }
@Override public final void setCustomErrorMessage(CharSequence customMessage) { Expectation expectation = getCurrentExpectation(); if (pendingError == null) { expectation.setCustomErrorMessage(customMessage); } else if (customMessage != null) { StackTraceElement[] previousStackTrace = pendingError.getStackTrace(); pendingError = new AssertionError(customMessage + "\n" + pendingError.getMessage()); pendingError.setStackTrace(previousStackTrace); } }