Skip to content

Commit eeea1c3

Browse files
keszoczeOliver Keszöcze
andauthored
Use AssertJ in error-handling (#2494)
* make error handling use AssertJ for exceptions * Adhere to coding style * Completely switch to AssertJ --------- Co-authored-by: Oliver Keszöcze <[email protected]>
1 parent 2c87b03 commit eeea1c3

File tree

1 file changed

+36
-62
lines changed

1 file changed

+36
-62
lines changed
Lines changed: 36 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
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.*;
62

73
import org.junit.Ignore;
84
import org.junit.Test;
@@ -15,116 +11,94 @@ public class ErrorHandlingTest {
1511

1612
@Test
1713
public void testThrowIllegalArgumentException() {
18-
assertThrows(
19-
IllegalArgumentException.class,
20-
errorHandling::handleErrorByThrowingIllegalArgumentException);
14+
assertThatExceptionOfType(Exception.class)
15+
.isThrownBy(() -> errorHandling.handleErrorByThrowingIllegalArgumentException());
2116
}
2217

2318
@Ignore("Remove to run test")
2419
@Test
2520
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.");
3425
}
3526

3627
@Ignore("Remove to run test")
3728
@Test
3829
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);
4433
}
4534

4635
@Ignore("Remove to run test")
4736
@Test
4837
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.");
5743
}
5844

5945
@Ignore("Remove to run test")
6046
@Test
6147
public void testThrowAnyUncheckedException() {
62-
assertThrows(
63-
RuntimeException.class,
64-
errorHandling::handleErrorByThrowingAnyUncheckedException);
48+
assertThatExceptionOfType(RuntimeException.class)
49+
.isThrownBy(() -> errorHandling.handleErrorByThrowingAnyUncheckedException());
6550
}
6651

6752
@Ignore("Remove to run test")
6853
@Test
6954
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.");
7759
}
7860

7961
@Ignore("Remove to run test")
8062
@Test
8163
public void testThrowCustomCheckedException() {
82-
assertThrows(
83-
CustomCheckedException.class,
84-
errorHandling::handleErrorByThrowingCustomCheckedException);
64+
assertThatExceptionOfType(CustomCheckedException.class)
65+
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomCheckedException());
8566
}
8667

8768
@Ignore("Remove to run test")
8869
@Test
8970
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.");
9775
}
9876

9977
@Ignore("Remove to run test")
10078
@Test
10179
public void testThrowCustomUncheckedException() {
102-
assertThrows(
103-
CustomUncheckedException.class,
104-
errorHandling::handleErrorByThrowingCustomUncheckedException);
80+
assertThatExceptionOfType(CustomUncheckedException.class)
81+
.isThrownBy(() -> errorHandling.handleErrorByThrowingCustomUncheckedException());
10582
}
10683

10784
@Ignore("Remove to run test")
10885
@Test
10986
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.");
11791
}
11892

11993
@Ignore("Remove to run test")
12094
@Test
12195
public void testReturnOptionalInstance() {
12296
Optional<Integer> successfulResult = errorHandling.handleErrorByReturningOptionalInstance("1");
123-
assertTrue(successfulResult.isPresent());
124-
assertEquals(1, (int) successfulResult.get());
97+
assertThat(successfulResult).isPresent().hasValue(1);
12598

12699
Optional<Integer> failureResult = errorHandling.handleErrorByReturningOptionalInstance("a");
127-
assertFalse(failureResult.isPresent());
100+
assertThat(failureResult).isNotPresent();
101+
128102
}
129103

130104
}

0 commit comments

Comments
 (0)