11package info .unterrainer .commons .jreutils ;
22
3+ import static org .junit .jupiter .api .Assertions .assertThrows ;
4+ import static org .junit .jupiter .api .Assertions .assertTrue ;
5+
36import org .junit .jupiter .api .Test ;
47
58public class ExceptionsTests {
69
710 @ Test
8- public void BeforeTest () {
11+ public void codeBeforeTest () {
912 try {
1013 // Some method you don't have control over throwing an exception.
1114 throw new IllegalArgumentException ();
@@ -15,9 +18,34 @@ public void BeforeTest() {
1518 }
1619
1720 @ Test
18- public void AfterTest () {
21+ public void throwsExceptionWithoutSwallow () {
22+ assertThrows (IllegalArgumentException .class , () -> {
23+ // Some method you don't have control over throwing an exception.
24+ throw new IllegalArgumentException ();
25+ });
26+ }
27+
28+ @ Test
29+ public void swallowsWhenExceptionMatches () {
1930 Exceptions .swallow (() -> {
2031 throw new IllegalArgumentException ();
2132 }, IllegalArgumentException .class , NumberFormatException .class );
33+ assertTrue (true );
34+ }
35+
36+ @ Test
37+ public void swallowsWhenCauseMatches () {
38+ Exceptions .swallow (() -> {
39+ throw new IllegalArgumentException ("" , new NumberFormatException ());
40+ }, NumberFormatException .class );
41+ assertTrue (true );
42+ }
43+
44+ @ Test
45+ public void swallowsWhenMultiLayeredCauseMatches () {
46+ Exceptions .swallow (() -> {
47+ throw new IllegalArgumentException ("" , new IllegalArgumentException ("" , new NumberFormatException ()));
48+ }, NumberFormatException .class );
49+ assertTrue (true );
2250 }
2351}
0 commit comments