static void fail(String message, Object expected, Object actual) { throw new AssertionFailedError(message, expected, actual); }
static DecoratedAssertionFailedError create(Throwable src) { // The two constructors have different behaviour due to // parent constructor org.opentest4j.DecoratedAssertionFailedError DecoratedAssertionFailedError result; if (src instanceof AssertionFailedError && ((AssertionFailedError) src).getExpected() != null && ((AssertionFailedError) src).getActual() != null) { result = new DecoratedAssertionFailedError( src.toString(), ((AssertionFailedError) src).getExpected().getValue(), ((AssertionFailedError) src).getActual().getValue(), src ); } else { result = new DecoratedAssertionFailedError( src.toString(), src ); } result.setStackTrace(src.getStackTrace()); for (Throwable sp : src.getSuppressed()) { result.addSuppressed(sp); } return result; }
private AssertionFailedError(String message, ValueWrapper expected, ValueWrapper actual, Throwable cause) { super((message == null || message.trim().length() == 0) ? "" : message); this.expected = expected; this.actual = actual; initCause(cause); }
/** * Returns a short description of this assertion error using the same format * as {@link Throwable#toString()}. * * @return Since the {@link AssertionError} superclass does not support * {@code null} messages and the constructors of this class convert supplied * {@code null} or blank messages to the empty {@code String}, this method * only includes non-empty messages in its return value. */ @Override public String toString() { String className = getClass().getName(); String message = getLocalizedMessage(); return ("".equals(message) ? className : (className + ": " + message)); }
static void fail() { throw new AssertionFailedError(); }
static void fail(String message) { throw new AssertionFailedError(message); }
static void fail(String message, Throwable cause) { throw new AssertionFailedError(message, cause); }
static void fail(Throwable cause) { throw new AssertionFailedError(null, cause); }
static void fail(Supplier<String> messageSupplier) { throw new AssertionFailedError(nullSafeGet(messageSupplier)); }
@Override public void handleTestExecutionException( ExtensionContext context, Throwable t ) { if ( t instanceof TestAbortedException ) { return; } final long seed = getStoredValue( context ).seed(); // The reason we throw a new exception wrapping the actual exception here, instead of simply enhancing the message is: // - AssertionFailedError has its own 'message' field, in addition to Throwable's 'detailedMessage' field // - Even if 'message' field is updated the test doesn't seem to print the updated message on assertion failure throw new AssertionFailedError( format( "%s [ random seed used: %dL ]", t.getMessage(), seed ), t ); } }
private static AssertionFailedError createAssertionFailedError(Object messageOrSupplier, Throwable t) { String message = buildPrefix(nullSafeGet(messageOrSupplier)) + "Unexpected exception thrown: " + t.getClass().getName() + buildSuffix(t.getMessage()); return new AssertionFailedError(message, t); }
@SuppressWarnings("unchecked") private static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable, Object messageOrSupplier) { try { executable.execute(); } catch (Throwable actualException) { if (expectedType.isInstance(actualException)) { return (T) actualException; } else { BlacklistedExceptions.rethrowIfBlacklisted(actualException); String message = buildPrefix(nullSafeGet(messageOrSupplier)) + format(expectedType, actualException.getClass(), "Unexpected exception type thrown"); throw new AssertionFailedError(message, actualException); } } String message = buildPrefix(nullSafeGet(messageOrSupplier)) + String.format("Expected %s to be thrown, but nothing was thrown.", getCanonicalName(expectedType)); throw new AssertionFailedError(message); }
throw new AssertionFailedError(buildPrefix(nullSafeGet(messageOrSupplier)) + "execution timed out after " + timeoutInMillis + " ms");
Throwable exception = listener.exceptions.get(0); if (!(exception instanceof AssertionFailedError)) { throw new AssertionFailedError( exception.getClass().getName() + " is not an instance of " + AssertionFailedError.class.getName(), exception);
/** * <em>Fails</em> a test with a given {@code message}. */ public static void fail(String message) { throw new AssertionFailedError(message); }
@Override public void handleTestExecutionException( ExtensionContext context, Throwable t ) { if ( t instanceof TestAbortedException ) { return; } final long seed = getStoredValue( context ).seed(); // The reason we throw a new exception wrapping the actual exception here, instead of simply enhancing the message is: // - AssertionFailedError has its own 'message' field, in addition to Throwable's 'detailedMessage' field // - Even if 'message' field is updated the test doesn't seem to print the updated message on assertion failure throw new AssertionFailedError( format( "%s [ random seed used: %dL ]", t.getMessage(), seed ), t ); } }
/** * Asserts that executing {@code executable} will throw an exception of type {@code expected}. * If no exception is thrown or if an exception of a different type is thrown, the method will fail. * * <p>The thrown exception instance is returned.</p> * * <p>Use {@code expectThrows} if you want to perform additional checks on the exception instance. * Otherwise use {@link #assertThrows(Class, Executable)}.</p> */ @SuppressWarnings("unchecked") public static <T extends Throwable> T expectThrows(Class<T> expectedType, Executable executable) { try { executable.execute(); } catch (Throwable actualException) { if (expectedType.isInstance(actualException)) { return (T) actualException; } else { String message = Assertions.format(expectedType.getName(), actualException.getClass().getName(), "Unexpected exception type thrown"); throw new AssertionFailedError(message, actualException); } } throw new AssertionFailedError( String.format("Expected %s to be thrown, but nothing was thrown.", expectedType.getName())); }
@Override public void afterTestExecution(ExtensionContext context) throws Exception { switch (loadExceptionStatus(context)) { case WAS_NOT_THROWN: //@formatter:off expectedException(context) .map(expected -> new AssertionFailedError(format(EXPECTED_EXCEPTION_WAS_NOT_THROWN, expected))) .ifPresent(error -> { throw error; }); //@formatter:on case WAS_THROWN_AS_EXPECTED: // the exception was thrown as expected so there is nothing to do break; case WAS_THROWN_NOT_AS_EXPECTED: // an exception was thrown but of the wrong type; // it was rethrown in `handleTestExecutionException` // so there is nothing to do here break; } }
@Override public void afterTestExecution(ExtensionContext context) throws Exception { switch (loadExceptionStatus(context)) { case WAS_NOT_THROWN: //@formatter:off expectedException(context) .map(expected -> new AssertionFailedError(format(EXPECTED_EXCEPTION_WAS_NOT_THROWN, expected))) .ifPresent(error -> { throw error; }); //@formatter:on case WAS_THROWN_AS_EXPECTED: // the exception was thrown as expected so there is nothing to do break; case WAS_THROWN_NOT_AS_EXPECTED: // an exception was thrown but of the wrong type; // it was rethrown in `handleTestExecutionException` // so there is nothing to do here break; } }
public static void rethrowAsJunit5Error(AssertionError error) { if (error.getCause() instanceof InvocationTargetException) { InvocationTargetException intellijInternal = (InvocationTargetException) error.getCause(); if (intellijInternal.getCause() instanceof FileComparisonFailure) { FileComparisonFailure fileComparisonFailure = ((FileComparisonFailure) intellijInternal.getCause()); String expected = fileComparisonFailure.getExpected(); String actual = fileComparisonFailure.getActual(); throw new AssertionFailedError("Expected file do not match actual file", expected, actual); } } throw error; }