static void fail(String message, Object expected, Object actual) { throw new AssertionFailedError(message, expected, actual); }
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; }
@Test public void testSubLanguagesIsTheSameSizeAsTheDefaultLanguage() { Set<String> defaultStrings = getKeys(I18n.getDefaultLanguage()); for (LanguageContainer entry : I18n.languages) { Set<String> strings = getKeys(entry); if (defaultStrings.size() != strings.size()) { getLogger().error("Comparing keys sizes for the default language and " + entry.getLanguage().getCode() + " failed!\n\nExpected:\t{}\nActual:\t\t{}\n", defaultStrings.size(), strings.size(), new AssertionFailedError() ); } } }