com.fitbur.assertj.api.ThrowableAssert Maven / Gradle / Ivy
The newest version!
/**
* Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
* an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the
* specific language governing permissions and limitations under the License.
*
* Copyright 2012-2016 the original author or authors.
*/
package com.fitbur.assertj.api;
import java.util.concurrent.Callable;
/**
* Assertion methods for {@link Throwable}s.
*
* To create a new instance of this class, invoke {@link Assertions#assertThat(Throwable)}
.
*
*
* @author David DIDIER
* @author Alex Ruiz
* @author Joel Costigliola
* @author Mikhail Mazursky
*/
public class ThrowableAssert extends AbstractThrowableAssert {
public interface ThrowingCallable {
void call() throws Throwable;
}
protected ThrowableAssert(Throwable actual) {
super(actual, ThrowableAssert.class);
}
protected ThrowableAssert(Callable runnable) {
super(buildThrowableAssertFromCallable(runnable), ThrowableAssert.class);
}
private static Throwable buildThrowableAssertFromCallable(Callable callable)
throws AssertionError {
try {
callable.call();
// fail if the expected exception was *not* thrown
Fail.fail("Expecting code to throw an exception.");
// this will *never* happen...
return null;
} catch (AssertionError e) {
// do not handle AssertionErrors in the next catch block!
throw e;
} catch (Throwable throwable) {
// the throwable we will check
return throwable;
}
}
public static Throwable catchThrowable(ThrowingCallable shouldRaiseThrowable) {
try {
shouldRaiseThrowable.call();
} catch (Throwable throwable) {
return throwable;
}
return null;
}
}