Skip to content

Commit

Permalink
Recommend assertThrows and assertFailsWith.
Browse files Browse the repository at this point in the history
#1292

RELNOTES=n/a
PiperOrigin-RevId: 633264611
  • Loading branch information
cpovirk authored and Google Java Core Libraries committed May 13, 2024
1 parent 0a2339b commit 75a84c2
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
14 changes: 14 additions & 0 deletions core/src/main/java/com/google/common/truth/ThrowableSubject.java
Expand Up @@ -22,6 +22,20 @@
/**
* Propositions for {@link Throwable} subjects.
*
* <p>Truth does not provide its own support for calling a method and automatically catching an
* expected exception, only for asserting on the exception after it has been caught. To catch the
* exception, we suggest {@link org.junit.Assert#assertThrows(Class,
* org.junit.function.ThrowingRunnable) assertThrows} (JUnit), <a
* href="https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-fails-with.html">{@code
* assertFailsWith}</a> ({@code kotlin.test}), or similar functionality from your testing library of
* choice.
*
* <pre>
* InvocationTargetException expected =
* assertThrows(InvocationTargetException.class, () -> method.invoke(null));
* assertThat(expected).hasCauseThat().isInstanceOf(IOException.class);
* </pre>
*
* @author Kurt Alfred Kluever
*/
public class ThrowableSubject extends Subject {
Expand Down
17 changes: 17 additions & 0 deletions core/src/main/java/com/google/common/truth/Truth.java
Expand Up @@ -168,6 +168,23 @@ public static ClassSubject assertThat(@Nullable Class<?> actual) {
return assert_().that(actual);
}

/**
* Begins an assertion about a {@link Throwable}.
*
* <p>Truth does not provide its own support for calling a method and automatically catching an
* expected exception, only for asserting on the exception after it has been caught. To catch the
* exception, we suggest {@link org.junit.Assert#assertThrows(Class,
* org.junit.function.ThrowingRunnable) assertThrows} (JUnit), <a
* href="https://kotlinlang.org/api/latest/kotlin.test/kotlin.test/assert-fails-with.html">{@code
* assertFailsWith}</a> ({@code kotlin.test}), or similar functionality from your testing library
* of choice.
*
* <pre>
* InvocationTargetException expected =
* assertThrows(InvocationTargetException.class, () -> method.invoke(null));
* assertThat(expected).hasCauseThat().isInstanceOf(IOException.class);
* </pre>
*/
public static ThrowableSubject assertThat(@Nullable Throwable actual) {
return assert_().that(actual);
}
Expand Down

0 comments on commit 75a84c2

Please sign in to comment.