1
- import static org .assertj .core .api .Assertions .assertThat ;
2
- import static org .junit .Assert .assertEquals ;
3
- import static org .junit .Assert .assertThrows ;
4
- import static org .junit .Assert .assertFalse ;
5
- import static org .junit .Assert .assertTrue ;
1
+ import static org .assertj .core .api .Assertions .*;
6
2
7
3
import org .junit .Ignore ;
8
4
import org .junit .Test ;
@@ -15,116 +11,94 @@ public class ErrorHandlingTest {
15
11
16
12
@ Test
17
13
public void testThrowIllegalArgumentException () {
18
- assertThrows (
19
- IllegalArgumentException .class ,
20
- errorHandling ::handleErrorByThrowingIllegalArgumentException );
14
+ assertThatExceptionOfType (Exception .class )
15
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingIllegalArgumentException ());
21
16
}
22
17
23
18
@ Ignore ("Remove to run test" )
24
19
@ Test
25
20
public void testThrowIllegalArgumentExceptionWithDetailMessage () {
26
- IllegalArgumentException expected =
27
- assertThrows (
28
- IllegalArgumentException .class ,
29
- () -> errorHandling
30
- .handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage (
31
- "This is the detail message." ));
32
-
33
- assertThat (expected ).hasMessage ("This is the detail message." );
21
+ assertThatExceptionOfType (IllegalArgumentException .class )
22
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage (
23
+ "This is the detail message." ))
24
+ .withMessage ("This is the detail message." );
34
25
}
35
26
36
27
@ Ignore ("Remove to run test" )
37
28
@ Test
38
29
public void testThrowAnyCheckedException () {
39
- Exception expected =
40
- assertThrows (
41
- Exception .class ,
42
- errorHandling ::handleErrorByThrowingAnyCheckedException );
43
- assertThat (expected ).isNotInstanceOf (RuntimeException .class );
30
+ assertThatExceptionOfType (Exception .class )
31
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingAnyCheckedException ())
32
+ .isNotInstanceOf (RuntimeException .class );
44
33
}
45
34
46
35
@ Ignore ("Remove to run test" )
47
36
@ Test
48
37
public void testThrowAnyCheckedExceptionWithDetailMessage () {
49
- Exception expected =
50
- assertThrows (
51
- Exception .class ,
52
- () -> errorHandling
53
- .handleErrorByThrowingAnyCheckedExceptionWithDetailMessage (
54
- "This is the detail message." ));
55
- assertThat (expected ).isNotInstanceOf (RuntimeException .class );
56
- assertThat (expected ).hasMessage ("This is the detail message." );
38
+ assertThatExceptionOfType (Exception .class )
39
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingAnyCheckedExceptionWithDetailMessage (
40
+ "This is the detail message." ))
41
+ .isNotInstanceOf (RuntimeException .class )
42
+ .withMessage ("This is the detail message." );
57
43
}
58
44
59
45
@ Ignore ("Remove to run test" )
60
46
@ Test
61
47
public void testThrowAnyUncheckedException () {
62
- assertThrows (
63
- RuntimeException .class ,
64
- errorHandling ::handleErrorByThrowingAnyUncheckedException );
48
+ assertThatExceptionOfType (RuntimeException .class )
49
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingAnyUncheckedException ());
65
50
}
66
51
67
52
@ Ignore ("Remove to run test" )
68
53
@ Test
69
54
public void testThrowAnyUncheckedExceptionWithDetailMessage () {
70
- RuntimeException expected =
71
- assertThrows (
72
- RuntimeException .class ,
73
- () -> errorHandling
74
- .handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage (
75
- "This is the detail message." ));
76
- assertThat (expected ).hasMessage ("This is the detail message." );
55
+ assertThatExceptionOfType (RuntimeException .class )
56
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage (
57
+ "This is the detail message." ))
58
+ .withMessage ("This is the detail message." );
77
59
}
78
60
79
61
@ Ignore ("Remove to run test" )
80
62
@ Test
81
63
public void testThrowCustomCheckedException () {
82
- assertThrows (
83
- CustomCheckedException .class ,
84
- errorHandling ::handleErrorByThrowingCustomCheckedException );
64
+ assertThatExceptionOfType (CustomCheckedException .class )
65
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingCustomCheckedException ());
85
66
}
86
67
87
68
@ Ignore ("Remove to run test" )
88
69
@ Test
89
70
public void testThrowCustomCheckedExceptionWithDetailMessage () {
90
- CustomCheckedException expected =
91
- assertThrows (
92
- CustomCheckedException .class ,
93
- () -> errorHandling
94
- .handleErrorByThrowingCustomCheckedExceptionWithDetailMessage (
95
- "This is the detail message." ));
96
- assertThat (expected ).hasMessage ("This is the detail message." );
71
+ assertThatExceptionOfType (CustomCheckedException .class )
72
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingCustomCheckedExceptionWithDetailMessage (
73
+ "This is the detail message." ))
74
+ .withMessage ("This is the detail message." );
97
75
}
98
76
99
77
@ Ignore ("Remove to run test" )
100
78
@ Test
101
79
public void testThrowCustomUncheckedException () {
102
- assertThrows (
103
- CustomUncheckedException .class ,
104
- errorHandling ::handleErrorByThrowingCustomUncheckedException );
80
+ assertThatExceptionOfType (CustomUncheckedException .class )
81
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingCustomUncheckedException ());
105
82
}
106
83
107
84
@ Ignore ("Remove to run test" )
108
85
@ Test
109
86
public void testThrowCustomUncheckedExceptionWithDetailMessage () {
110
- CustomUncheckedException expected =
111
- assertThrows (
112
- CustomUncheckedException .class ,
113
- () -> errorHandling
114
- .handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage (
115
- "This is the detail message." ));
116
- assertThat (expected ).hasMessage ("This is the detail message." );
87
+ assertThatExceptionOfType (CustomUncheckedException .class )
88
+ .isThrownBy (() -> errorHandling .handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage (
89
+ "This is the detail message." ))
90
+ .withMessage ("This is the detail message." );
117
91
}
118
92
119
93
@ Ignore ("Remove to run test" )
120
94
@ Test
121
95
public void testReturnOptionalInstance () {
122
96
Optional <Integer > successfulResult = errorHandling .handleErrorByReturningOptionalInstance ("1" );
123
- assertTrue (successfulResult .isPresent ());
124
- assertEquals (1 , (int ) successfulResult .get ());
97
+ assertThat (successfulResult ).isPresent ().hasValue (1 );
125
98
126
99
Optional <Integer > failureResult = errorHandling .handleErrorByReturningOptionalInstance ("a" );
127
- assertFalse (failureResult .isPresent ());
100
+ assertThat (failureResult ).isNotPresent ();
101
+
128
102
}
129
103
130
104
}
0 commit comments