33import org .hamcrest .AbstractMatcherTest ;
44import org .hamcrest .Matcher ;
55
6+ import static org .hamcrest .MatcherAssert .assertThat ;
67import static org .hamcrest .Matchers .containsString ;
78import static org .hamcrest .Matchers .instanceOf ;
89import static org .hamcrest .exception .ThrowsException .throwsException ;
@@ -16,7 +17,23 @@ private Runnable runnableThrowing(Throwable exception) {
1617
1718 @ Override
1819 protected Matcher <?> createMatcher () {
19- return throwsException (new IllegalArgumentException ("Boom!" ));
20+ return throwsException (IllegalArgumentException .class );
21+ }
22+
23+ public void testExamples () {
24+ RuntimeException boom = new RuntimeException ("boom" );
25+ Runnable codeThatThrows = () -> {
26+ throw boom ;
27+ };
28+
29+ assertThat (codeThatThrows , throwsException ());
30+ assertThat (codeThatThrows , throwsException (boom ));
31+ assertThat (codeThatThrows , throwsException (RuntimeException .class ));
32+ assertThat (codeThatThrows , throwsException (withMessage ("boom" )));
33+ assertThat (codeThatThrows , throwsException (withMessage (containsString ("oom" ))));
34+ assertThat (codeThatThrows , throwsException (RuntimeException .class , "boom" ));
35+ assertThat (codeThatThrows , throwsException (RuntimeException .class , withMessage ("boom" )));
36+ assertThat (codeThatThrows , throwsException (RuntimeException .class , withMessage (containsString ("boom" ))));
2037 }
2138
2239 public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage () {
@@ -45,7 +62,8 @@ public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMess
4562 assertMismatchDescription (
4663 "the runnable didn't throw" ,
4764 throwsException (new IllegalArgumentException ("Boom!" )),
48- (Runnable ) () -> {}
65+ (Runnable ) () -> {
66+ }
4967 );
5068 }
5169
0 commit comments