|
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+import org.junit.Before;
|
|
|
2
|
+import org.junit.Ignore;
|
|
|
3
|
+import org.junit.Rule;
|
|
|
4
|
+import org.junit.Test;
|
|
|
5
|
+import org.junit.rules.ExpectedException;
|
|
|
6
|
+
|
|
|
7
|
+import static org.junit.Assert.assertEquals;
|
|
|
8
|
+import static org.junit.Assert.assertFalse;
|
|
|
9
|
+import static org.junit.Assert.assertTrue;
|
|
|
10
|
+
|
|
|
11
|
+import java.util.Optional;
|
|
|
12
|
+
|
|
|
13
|
+public class ErrorHandlingTest {
|
|
|
14
|
+
|
|
|
15
|
+ private ErrorHandling errorHandling;
|
|
|
16
|
+
|
|
|
17
|
+ @Rule
|
|
|
18
|
+ public ExpectedException thrown = ExpectedException.none();
|
|
|
19
|
+
|
|
|
20
|
+ @Before
|
|
|
21
|
+ public void setUp() {
|
|
|
22
|
+ errorHandling = new ErrorHandling();
|
|
|
23
|
+ }
|
|
|
24
|
+
|
|
|
25
|
+ @Test
|
|
|
26
|
+ public void testThrowIllegalArgumentException() {
|
|
|
27
|
+ thrown.expect(IllegalArgumentException.class);
|
|
|
28
|
+ errorHandling.handleErrorByThrowingIllegalArgumentException();
|
|
|
29
|
+ }
|
|
|
30
|
+
|
|
|
31
|
+ @Ignore("Remove to run test")
|
|
|
32
|
+ @Test
|
|
|
33
|
+ public void testThrowIllegalArgumentExceptionWithDetailMessage() {
|
|
|
34
|
+ thrown.expect(IllegalArgumentException.class);
|
|
|
35
|
+ thrown.expectMessage("This is the detail message.");
|
|
|
36
|
+ errorHandling.handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage("This is the detail message.");
|
|
|
37
|
+ }
|
|
|
38
|
+
|
|
|
39
|
+ @Ignore("Remove to run test")
|
|
|
40
|
+ @Test
|
|
|
41
|
+ public void testThrowAnyCheckedException() {
|
|
|
42
|
+ try {
|
|
|
43
|
+ errorHandling.handleErrorByThrowingAnyCheckedException();
|
|
|
44
|
+ } catch (Exception e) {
|
|
|
45
|
+ assertFalse(e instanceof RuntimeException);
|
|
|
46
|
+ }
|
|
|
47
|
+ }
|
|
|
48
|
+
|
|
|
49
|
+ @Ignore("Remove to run test")
|
|
|
50
|
+ @Test
|
|
|
51
|
+ public void testThrowAnyCheckedExceptionWithDetailMessage() {
|
|
|
52
|
+ try {
|
|
|
53
|
+ errorHandling.handleErrorByThrowingAnyCheckedExceptionWithDetailMessage("This is the detail message.");
|
|
|
54
|
+ } catch (Exception e) {
|
|
|
55
|
+ assertFalse(e instanceof RuntimeException);
|
|
|
56
|
+ assertEquals("This is the detail message.", e.getMessage());
|
|
|
57
|
+ }
|
|
|
58
|
+ }
|
|
|
59
|
+
|
|
|
60
|
+ @Ignore("Remove to run test")
|
|
|
61
|
+ @Test
|
|
|
62
|
+ public void testThrowAnyUncheckedException() {
|
|
|
63
|
+ thrown.expect(RuntimeException.class);
|
|
|
64
|
+ errorHandling.handleErrorByThrowingAnyUncheckedException();
|
|
|
65
|
+ }
|
|
|
66
|
+
|
|
|
67
|
+ @Ignore("Remove to run test")
|
|
|
68
|
+ @Test
|
|
|
69
|
+ public void testThrowAnyUncheckedExceptionWithDetailMessage() {
|
|
|
70
|
+ thrown.expect(RuntimeException.class);
|
|
|
71
|
+ thrown.expectMessage("This is the detail message.");
|
|
|
72
|
+ errorHandling.handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage("This is the detail message.");
|
|
|
73
|
+ }
|
|
|
74
|
+
|
|
|
75
|
+ @Ignore("Remove to run test")
|
|
|
76
|
+ @Test
|
|
|
77
|
+ public void testThrowCustomCheckedException() throws CustomCheckedException {
|
|
|
78
|
+ thrown.expect(CustomCheckedException.class);
|
|
|
79
|
+ errorHandling.handleErrorByThrowingCustomCheckedException();
|
|
|
80
|
+ }
|
|
|
81
|
+
|
|
|
82
|
+ @Ignore("Remove to run test")
|
|
|
83
|
+ @Test
|
|
|
84
|
+ public void testThrowCustomCheckedExceptionWithDetailMessage() throws CustomCheckedException {
|
|
|
85
|
+ thrown.expect(CustomCheckedException.class);
|
|
|
86
|
+ thrown.expectMessage("This is the detail message.");
|
|
|
87
|
+ errorHandling.handleErrorByThrowingCustomCheckedExceptionWithDetailMessage("This is the detail message.");
|
|
|
88
|
+ }
|
|
|
89
|
+
|
|
|
90
|
+ @Ignore("Remove to run test")
|
|
|
91
|
+ @Test
|
|
|
92
|
+ public void testThrowCustomUncheckedException() {
|
|
|
93
|
+ thrown.expect(CustomUncheckedException.class);
|
|
|
94
|
+ errorHandling.handleErrorByThrowingCustomUncheckedException();
|
|
|
95
|
+ }
|
|
|
96
|
+
|
|
|
97
|
+ @Ignore("Remove to run test")
|
|
|
98
|
+ @Test
|
|
|
99
|
+ public void testThrowCustomUncheckedExceptionWithDetailMessage() {
|
|
|
100
|
+ thrown.expect(CustomUncheckedException.class);
|
|
|
101
|
+ thrown.expectMessage("This is the detail message.");
|
|
|
102
|
+ errorHandling.handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage("This is the detail message.");
|
|
|
103
|
+ }
|
|
|
104
|
+
|
|
|
105
|
+ @Ignore("Remove to run test")
|
|
|
106
|
+ @Test
|
|
|
107
|
+ public void testReturnOptionalInstance() {
|
|
|
108
|
+ Optional<Integer> successfulResult = errorHandling.handleErrorByReturningOptionalInstance("1");
|
|
|
109
|
+ assertTrue(successfulResult.isPresent());
|
|
|
110
|
+ assertEquals(1, (int) successfulResult.get());
|
|
|
111
|
+
|
|
|
112
|
+ Optional<Integer> failureResult = errorHandling.handleErrorByReturningOptionalInstance("a");
|
|
|
113
|
+ assertFalse(failureResult.isPresent());
|
|
|
114
|
+ }
|
|
|
115
|
+
|
|
|
116
|
+}
|