11package org .hamcrest .exception ;
22
3- import org .hamcrest .AbstractMatcherTest ;
4- import org .hamcrest .Matcher ;
3+ import org .junit .jupiter .api .Test ;
54
65import static org .hamcrest .MatcherAssert .assertThat ;
76import static org .hamcrest .Matchers .containsString ;
87import static org .hamcrest .Matchers .instanceOf ;
98import static org .hamcrest .exception .ThrowsException .throwsException ;
109import static org .hamcrest .exception .ThrowsExceptionWithMessage .withMessage ;
11-
12- public class ThrowsExceptionEqualTest extends AbstractMatcherTest {
13-
14- private Runnable runnableThrowing (Throwable exception ) {
15- return new ThrowingRunnable (exception );
16- }
17-
18- @ Override
19- protected Matcher <?> createMatcher () {
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" ))));
37- }
38-
39- public void testEvaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage () {
40- assertMatches (
41- throwsException (new IllegalArgumentException ("Boom!" )),
42- runnableThrowing (new IllegalArgumentException ("Boom!" ))
43- );
44-
45- assertDescription (
46- "a runnable throwing \" java.lang.IllegalArgumentException\" with message \" Boom!\" " ,
47- throwsException (new IllegalArgumentException ("Boom!" ))
48- );
49-
50- assertMismatchDescription (
51- "the runnable threw \" java.lang.IllegalArgumentException\" with message \" Bang!\" instead of \" Boom!\" " ,
52- throwsException (new IllegalArgumentException ("Boom!" )),
53- runnableThrowing (new IllegalArgumentException ("Bang!" ))
54- );
55-
56- assertMismatchDescription (
57- "the runnable threw \" java.lang.NullPointerException\" instead of a \" java.lang.IllegalArgumentException\" exception" ,
58- throwsException (new IllegalArgumentException ("Boom!" )),
59- runnableThrowing (new NullPointerException ("Boom!" ))
60- );
61-
62- assertMismatchDescription (
63- "the runnable didn't throw" ,
64- throwsException (new IllegalArgumentException ("Boom!" )),
65- (Runnable ) () -> {
66- }
67- );
68- }
69-
70- public void testEvaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage () {
71- assertMatches (
72- throwsException (new IllegalArgumentException ("Boom!" )),
73- runnableThrowing (new TestException ("Boom!" ))
74- );
75- }
76-
77- public void testEvaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage () {
78- assertMatches (
79- throwsException (withMessage ("Boom!" )),
80- runnableThrowing (new TestException ("Boom!" ))
81- );
82-
83- assertDescription (
84- "a runnable throwing an exception with message \" Boom!\" " ,
85- throwsException (withMessage ("Boom!" ))
86- );
87-
88- assertMismatchDescription (
89- "the runnable threw an exception with message \" Bang!\" instead of \" Boom!\" " ,
90- throwsException (withMessage ("Boom!" )),
91- runnableThrowing (new IllegalArgumentException ("Bang!" ))
92- );
10+ import static org .hamcrest .test .MatcherAssertions .*;
11+
12+ public final class ThrowsExceptionEqualTest {
13+
14+ private Runnable runnableThrowing (Throwable exception ) {
15+ return new ThrowingRunnable (exception );
16+ }
17+
18+ @ Test
19+ public void examples () {
20+ RuntimeException boom = new RuntimeException ("boom" );
21+ Runnable codeThatThrows = () -> {
22+ throw boom ;
23+ };
24+
25+ assertThat (codeThatThrows , throwsException ());
26+ assertThat (codeThatThrows , throwsException (boom ));
27+ assertThat (codeThatThrows , throwsException (RuntimeException .class ));
28+ assertThat (codeThatThrows , throwsException (withMessage ("boom" )));
29+ assertThat (codeThatThrows , throwsException (withMessage (containsString ("oom" ))));
30+ assertThat (codeThatThrows , throwsException (RuntimeException .class , "boom" ));
31+ assertThat (codeThatThrows , throwsException (RuntimeException .class , withMessage ("boom" )));
32+ assertThat (codeThatThrows , throwsException (RuntimeException .class , withMessage (containsString ("boom" ))));
33+ }
34+
35+ @ Test
36+ public void evaluatesToTrueIfRunnableThrowsExpectedExceptionWithMatchingMessage () {
37+ assertMatches (
38+ throwsException (new IllegalArgumentException ("Boom!" )),
39+ runnableThrowing (new IllegalArgumentException ("Boom!" ))
40+ );
41+
42+ assertDescription (
43+ "a runnable throwing \" java.lang.IllegalArgumentException\" with message \" Boom!\" " ,
44+ throwsException (new IllegalArgumentException ("Boom!" ))
45+ );
46+
47+ assertMismatchDescription (
48+ "the runnable threw \" java.lang.IllegalArgumentException\" with message \" Bang!\" instead of \" Boom!\" " ,
49+ throwsException (new IllegalArgumentException ("Boom!" )),
50+ runnableThrowing (new IllegalArgumentException ("Bang!" ))
51+ );
52+
53+ assertMismatchDescription (
54+ "the runnable threw \" java.lang.NullPointerException\" instead of a \" java.lang.IllegalArgumentException\" exception" ,
55+ throwsException (new IllegalArgumentException ("Boom!" )),
56+ runnableThrowing (new NullPointerException ("Boom!" ))
57+ );
58+
59+ assertMismatchDescription (
60+ "the runnable didn't throw" ,
61+ throwsException (new IllegalArgumentException ("Boom!" )),
62+ (Runnable ) () -> {
63+ }
64+ );
65+ }
66+
67+ @ Test
68+ public void evaluatesToTrueIfRunnableThrowsExceptionExtendingTheExpectedExceptionWithMatchingMessage () {
69+ assertMatches (
70+ throwsException (new IllegalArgumentException ("Boom!" )),
71+ runnableThrowing (new TestException ("Boom!" ))
72+ );
73+ }
74+
75+ @ Test
76+ public void evaluatesToTrueIfRunnableThrowsExceptionWithExpectedMessage () {
77+ assertMatches (
78+ throwsException (withMessage ("Boom!" )),
79+ runnableThrowing (new TestException ("Boom!" ))
80+ );
81+
82+ assertDescription (
83+ "a runnable throwing an exception with message \" Boom!\" " ,
84+ throwsException (withMessage ("Boom!" ))
85+ );
86+
87+ assertMismatchDescription (
88+ "the runnable threw an exception with message \" Bang!\" instead of \" Boom!\" " ,
89+ throwsException (withMessage ("Boom!" )),
90+ runnableThrowing (new IllegalArgumentException ("Bang!" ))
91+ );
92+ }
93+
94+ @ Test
95+ public void evaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage () {
96+
97+ assertMatches (
98+ throwsException (withMessage (containsString ("bar" ))),
99+ runnableThrowing (new TestException ("Foo bar baz" ))
100+ );
101+
102+ assertDescription (
103+ "a runnable throwing an exception with message a string containing \" bar\" " ,
104+ throwsException (withMessage (containsString ("bar" )))
105+ );
106+
107+ assertMismatchDescription (
108+ "the runnable threw an exception with message \" Bang!\" instead of a string containing \" bar\" " ,
109+ throwsException (withMessage (containsString ("bar" ))),
110+ runnableThrowing (new IllegalArgumentException ("Bang!" ))
111+ );
112+ }
113+
114+ @ Test
115+ public void evaluatesToTrueIfRunnableThrowsMatchingException () {
116+ assertMatches (
117+ throwsException (instanceOf (TestException .class )),
118+ runnableThrowing (new TestException ("Boom!" ))
119+ );
120+
121+ assertDescription (
122+ "a runnable throwing an instance of java.lang.NullPointerException" ,
123+ throwsException (instanceOf (NullPointerException .class ))
124+ );
125+
126+ assertMismatchDescription (
127+ "the runnable threw <java.lang.IllegalArgumentException: Bang!> is a java.lang.IllegalArgumentException" ,
128+ throwsException (instanceOf (NullPointerException .class )),
129+ runnableThrowing (new IllegalArgumentException ("Bang!" ))
130+ );
131+ }
132+
133+ public static class TestException extends IllegalArgumentException {
134+ public TestException (String message ) {
135+ super (message );
93136 }
137+ }
94138
95- public void testEvaluatesToTrueIfRunnableThrowsExceptionWithMatchingMessage () {
96-
97- assertMatches (
98- throwsException (withMessage (containsString ("bar" ))),
99- runnableThrowing (new TestException ("Foo bar baz" ))
100- );
139+ static class ThrowingRunnable implements Runnable {
140+ private final Throwable throwable ;
101141
102- assertDescription (
103- "a runnable throwing an exception with message a string containing \" bar\" " ,
104- throwsException (withMessage (containsString ("bar" )))
105- );
106-
107- assertMismatchDescription (
108- "the runnable threw an exception with message \" Bang!\" instead of a string containing \" bar\" " ,
109- throwsException (withMessage (containsString ("bar" ))),
110- runnableThrowing (new IllegalArgumentException ("Bang!" ))
111- );
142+ ThrowingRunnable (Throwable throwable ) {
143+ this .throwable = throwable ;
112144 }
113145
114- public void testEvaluatesToTrueIfRunnableThrowsMatchingException () {
115- assertMatches (
116- throwsException (instanceOf (TestException .class )),
117- runnableThrowing (new TestException ("Boom!" ))
118- );
119-
120- assertDescription (
121- "a runnable throwing an instance of java.lang.NullPointerException" ,
122- throwsException (instanceOf (NullPointerException .class ))
123- );
124-
125- assertMismatchDescription (
126- "the runnable threw <java.lang.IllegalArgumentException: Bang!> is a java.lang.IllegalArgumentException" ,
127- throwsException (instanceOf (NullPointerException .class )),
128- runnableThrowing (new IllegalArgumentException ("Bang!" ))
129- );
130- }
131-
132- public static class TestException extends IllegalArgumentException {
133- public TestException (String message ) {
134- super (message );
135- }
146+ @ Override
147+ public void run () {
148+ sneakyThrow (throwable );
136149 }
137150
138- static class ThrowingRunnable implements Runnable {
139- private final Throwable throwable ;
140-
141- ThrowingRunnable (Throwable throwable ) {
142- this .throwable = throwable ;
143- }
144-
145- @ Override
146- public void run () {
147- sneakyThrow (throwable );
148- }
149-
150- @ SuppressWarnings ("unchecked" )
151- private <T extends Throwable > void sneakyThrow (Throwable throwable ) throws T {
152- throw (T ) throwable ;
153- }
151+ @ SuppressWarnings ("unchecked" )
152+ private <T extends Throwable > void sneakyThrow (Throwable throwable ) throws T {
153+ throw (T ) throwable ;
154154 }
155+ }
155156}
0 commit comments