org.junit.jupiter.api.AssertThrows Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of junit-jupiter-api Show documentation
Show all versions of junit-jupiter-api Show documentation
Module "junit-jupiter-api" of JUnit 5.
/*
* Copyright 2015-2016 the original author or authors.
*
* All rights reserved. This program and the accompanying materials are
* made available under the terms of the Eclipse Public License v1.0 which
* accompanies this distribution and is available at
*
* http://www.eclipse.org/legal/epl-v10.html
*/
package org.junit.jupiter.api;
import static org.junit.jupiter.api.AssertionUtils.format;
import org.junit.jupiter.api.function.Executable;
import org.opentest4j.AssertionFailedError;
/**
* {@code AssertThrows} is a collection of utility methods that support asserting
* an exception of an expected type is thrown.
*
* @since 5.0
*/
class AssertThrows {
static T expectThrows(Class expectedType, Executable executable) {
return assertThrows(expectedType, executable);
}
@SuppressWarnings("unchecked")
static T assertThrows(Class extends Throwable> expectedType, Executable executable) {
try {
executable.execute();
}
catch (Throwable actualException) {
if (expectedType.isInstance(actualException)) {
return (T) actualException;
}
else {
String message = 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()));
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy