forked from exercism/java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathErrorHandlingTest.java
More file actions
117 lines (99 loc) · 4.83 KB
/
ErrorHandlingTest.java
File metadata and controls
117 lines (99 loc) · 4.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
public class ErrorHandlingTest {
private ErrorHandling errorHandling = new ErrorHandling();
@Test
@DisplayName("Throws IllegalArgumentException")
public void testThrowIllegalArgumentException() {
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentException());
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws IllegalArgumentException with provided detail message")
public void testThrowIllegalArgumentExceptionWithDetailMessage() {
assertThatExceptionOfType(IllegalArgumentException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentExceptionWithDetailMessage(
"This is the detail message."))
.withMessage("This is the detail message.");
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws any checked exception")
public void testThrowAnyCheckedException() {
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyCheckedException())
.isNotInstanceOf(RuntimeException.class);
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws any checked exception with provided detail message")
public void testThrowAnyCheckedExceptionWithDetailMessage() {
assertThatExceptionOfType(Exception.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyCheckedExceptionWithDetailMessage(
"This is the detail message."))
.isNotInstanceOf(RuntimeException.class)
.withMessage("This is the detail message.");
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws any unchecked exception")
public void testThrowAnyUncheckedException() {
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyUncheckedException());
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws any unchecked exception with provided detail message")
public void testThrowAnyUncheckedExceptionWithDetailMessage() {
assertThatExceptionOfType(RuntimeException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyUncheckedExceptionWithDetailMessage(
"This is the detail message."))
.withMessage("This is the detail message.");
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws custom checked exception")
public void testThrowCustomCheckedException() {
assertThatExceptionOfType(CustomCheckedException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomCheckedException());
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws custom checked exception with provided detail message")
public void testThrowCustomCheckedExceptionWithDetailMessage() {
assertThatExceptionOfType(CustomCheckedException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomCheckedExceptionWithDetailMessage(
"This is the detail message."))
.withMessage("This is the detail message.");
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws custom unchecked exception")
public void testThrowCustomUncheckedException() {
assertThatExceptionOfType(CustomUncheckedException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomUncheckedException());
}
@Disabled("Remove to run test")
@Test
@DisplayName("Throws custom unchecked exception with provided detail message")
public void testThrowCustomUncheckedExceptionWithDetailMessage() {
assertThatExceptionOfType(CustomUncheckedException.class)
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomUncheckedExceptionWithDetailMessage(
"This is the detail message."))
.withMessage("This is the detail message.");
}
@Disabled("Remove to run test")
@Test
@DisplayName("Handles error by throwing Optional instance")
public void testReturnOptionalInstance() {
Optional<Integer> successfulResult = errorHandling.handleErrorByReturningOptionalInstance("1");
assertThat(successfulResult).isPresent().hasValue(1);
Optional<Integer> failureResult = errorHandling.handleErrorByReturningOptionalInstance("a");
assertThat(failureResult).isNotPresent();
}
}