11package org .baeldung .guava ;
22
3- import static org .assertj .core .api .Assertions .assertThat ;
3+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
44import java .util .Arrays ;
55import org .junit .Test ;
66import static com .google .common .base .Preconditions .*;
@@ -10,177 +10,110 @@ public class GuavaPreConditionsTest {
1010 @ Test
1111 public void whenCheckArgumentEvaluatesFalse_throwsException () {
1212 int age = -18 ;
13- try {
14- checkArgument (age > 0 );
15- } catch (Exception exception ) {
16- assertThat (exception ).isInstanceOf (IllegalArgumentException .class );
17- assertThat (exception ).hasNoCause ();
18- assertThat (exception ).hasMessage (null );
19- }
13+
14+ assertThatThrownBy (() -> checkArgument (age > 0 )).isInstanceOf (IllegalArgumentException .class ).hasMessage (null ).hasNoCause ();
2015 }
2116
2217 @ Test
2318 public void givenErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException () {
2419 final int age = -18 ;
2520 final String message = "Age can't be zero or less than zero" ;
26- try {
27- checkArgument (age > 0 , message );
28- } catch (Exception exception ) {
29- assertThat (exception ).isInstanceOf (IllegalArgumentException .class );
30- assertThat (exception ).hasNoCause ();
31- assertThat (exception ).hasMessage (message );
32- }
21+
22+ assertThatThrownBy (() -> checkArgument (age > 0 , message )).isInstanceOf (IllegalArgumentException .class ).hasMessage (message ).hasNoCause ();
3323 }
3424
3525 @ Test
3626 public void givenTemplatedErrorMessage_whenCheckArgumentEvaluatesFalse_throwsException () {
3727 final int age = -18 ;
3828 final String message = "Age can't be zero or less than zero, you supplied %s." ;
39- try {
40- checkArgument (age > 0 , message , age );
41- } catch (Exception exception ) {
42- assertThat (exception ).isInstanceOf (IllegalArgumentException .class );
43- assertThat (exception ).hasNoCause ();
44- assertThat (exception ).hasMessage (message , age );
45- }
29+
30+ assertThatThrownBy (() -> checkArgument (age > 0 , message , age )).isInstanceOf (IllegalArgumentException .class ).hasMessage (message , age ).hasNoCause ();
4631 }
4732
4833 @ Test
4934 public void givenArrayOfIntegers_whenCheckElementIndexEvaluatesFalse_throwsException () {
5035 final int [] numbers = { 1 , 2 , 3 , 4 , 5 };
51- try {
52- checkElementIndex (6 , numbers .length - 1 );
53- } catch (Exception exception ) {
54- assertThat (exception ).isInstanceOf (IndexOutOfBoundsException .class );
55- assertThat (exception ).hasNoCause ();
56- }
36+
37+ assertThatThrownBy (() -> checkElementIndex (6 , numbers .length - 1 )).isInstanceOf (IndexOutOfBoundsException .class ).hasNoCause ();
5738 }
5839
5940 @ Test
6041 public void givenArrayOfIntegersAndMessage_whenCheckElementIndexEvaluatesFalse_throwsException () {
6142 final int [] numbers = { 1 , 2 , 3 , 4 , 5 };
6243 final String message = "Please check the bound of an array and retry" ;
63- try {
64- checkElementIndex (6 , numbers .length - 1 , message );
65- } catch (Exception exception ) {
66- assertThat (exception ).isInstanceOf (IndexOutOfBoundsException .class );
67- assertThat (exception ).hasNoCause ();
68- assertThat (exception .getMessage ()).startsWith (message );
69- }
44+
45+ assertThatThrownBy (() -> checkElementIndex (6 , numbers .length - 1 , message )).isInstanceOf (IndexOutOfBoundsException .class ).hasMessageStartingWith (message ).hasNoCause ();
7046 }
7147
7248 @ Test
7349 public void givenNullString_whenCheckNotNullCalled_throwsException () {
7450 final String nullObject = null ;
75- try {
76- checkNotNull (nullObject );
77- } catch (Exception exception ) {
78- assertThat (exception ).isInstanceOf (NullPointerException .class );
79- assertThat (exception ).hasNoCause ();
80- assertThat (exception ).hasMessage (null );
81- }
51+
52+ assertThatThrownBy (() -> checkNotNull (nullObject )).isInstanceOf (NullPointerException .class ).hasMessage (null ).hasNoCause ();
8253 }
8354
8455 @ Test
8556 public void givenNullString_whenCheckNotNullCalledWithMessage_throwsException () {
8657 final String nullObject = null ;
8758 final String message = "Please check the Object supplied, its null!" ;
88- try {
89- checkNotNull (nullObject , message );
90- } catch (Exception exception ) {
91- assertThat (exception ).isInstanceOf (NullPointerException .class );
92- assertThat (exception ).hasNoCause ();
93- assertThat (exception ).hasMessage (message );
94- }
59+
60+ assertThatThrownBy (() -> checkNotNull (nullObject , message )).isInstanceOf (NullPointerException .class ).hasMessage (message ).hasNoCause ();
9561 }
9662
9763 @ Test
9864 public void givenNullString_whenCheckNotNullCalledWithTemplatedMessage_throwsException () {
9965 final String nullObject = null ;
10066 final String message = "Please check the Object supplied, its %s!" ;
101- try {
102- checkNotNull (nullObject , message , nullObject );
103- } catch (Exception exception ) {
104- assertThat (exception ).isInstanceOf (NullPointerException .class );
105- assertThat (exception ).hasNoCause ();
106- assertThat (exception ).hasMessage (message , nullObject );
107- }
67+
68+ assertThatThrownBy (() -> checkNotNull (nullObject , message , nullObject )).isInstanceOf (NullPointerException .class ).hasMessage (message , nullObject ).hasNoCause ();
10869 }
10970
11071 @ Test
11172 public void givenArrayOfIntegers_whenCheckPositionIndexEvaluatesFalse_throwsException () {
11273 final int [] numbers = { 1 , 2 , 3 , 4 , 5 };
113- try {
114- checkPositionIndex (6 , numbers .length - 1 );
115- } catch (Exception exception ) {
116- assertThat (exception ).isInstanceOf (IndexOutOfBoundsException .class );
117- assertThat (exception ).hasNoCause ();
118- }
74+
75+ assertThatThrownBy (() -> checkPositionIndex (6 , numbers .length - 1 )).isInstanceOf (IndexOutOfBoundsException .class ).hasNoCause ();
11976 }
12077
12178 @ Test
12279 public void givenArrayOfIntegersAndMessage_whenCheckPositionIndexEvaluatesFalse_throwsException () {
12380 final int [] numbers = { 1 , 2 , 3 , 4 , 5 };
12481 final String message = "Please check the bound of an array and retry" ;
125- try {
126- checkPositionIndex (6 , numbers .length - 1 , message );
127- } catch (Exception exception ) {
128- assertThat (exception ).isInstanceOf (IndexOutOfBoundsException .class );
129- assertThat (exception ).hasNoCause ();
130- assertThat (exception .getMessage ()).startsWith (message );
131- }
82+
83+ assertThatThrownBy (() -> checkPositionIndex (6 , numbers .length - 1 , message )).isInstanceOf (IndexOutOfBoundsException .class ).hasMessageStartingWith (message ).hasNoCause ();
13284 }
13385
13486 @ Test
13587 public void givenArrayOfIntegers_whenCheckPositionIndexesEvaluatesFalse_throwsException () {
13688 final int [] numbers = { 1 , 2 , 3 , 4 , 5 };
137- try {
138- checkPositionIndexes (6 , 0 , numbers .length - 1 );
139- } catch (Exception exception ) {
140- assertThat (exception ).isInstanceOf (IndexOutOfBoundsException .class );
141- assertThat (exception ).hasNoCause ();
142- }
89+
90+ assertThatThrownBy (() -> checkPositionIndexes (6 , 0 , numbers .length - 1 )).isInstanceOf (IndexOutOfBoundsException .class ).hasNoCause ();
14391 }
14492
14593 @ Test
14694 public void givenValidStates_whenCheckStateEvaluatesFalse_throwsException () {
14795 final int [] validStates = { -1 , 0 , 1 };
14896 final int givenState = 10 ;
149- try {
150- checkState (Arrays .binarySearch (validStates , givenState ) > 0 );
151- } catch (Exception exception ) {
152- assertThat (exception ).isInstanceOf (IllegalStateException .class );
153- assertThat (exception ).hasNoCause ();
154- assertThat (exception ).hasMessage (null );
155- }
97+
98+ assertThatThrownBy (() -> checkState (Arrays .binarySearch (validStates , givenState ) > 0 )).isInstanceOf (IllegalStateException .class ).hasMessage (null ).hasNoCause ();
15699 }
157100
158101 @ Test
159102 public void givenValidStatesAndMessage_whenCheckStateEvaluatesFalse_throwsException () {
160103 final int [] validStates = { -1 , 0 , 1 };
161104 final int givenState = 10 ;
162105 final String message = "You have entered an invalid state" ;
163- try {
164- checkState (Arrays .binarySearch (validStates , givenState ) < 0 , message );
165- } catch (Exception exception ) {
166- assertThat (exception ).isInstanceOf (IllegalStateException .class );
167- assertThat (exception ).hasNoCause ();
168- assertThat (exception ).hasMessage (message );
169- }
106+
107+ assertThatThrownBy (() -> checkState (Arrays .binarySearch (validStates , givenState ) > 0 , message )).isInstanceOf (IllegalStateException .class ).hasMessageStartingWith (message ).hasNoCause ();
170108 }
171109
172110 @ Test
173111 public void givenValidStatesAndTemplatedMessage_whenCheckStateEvaluatesFalse_throwsException () {
174112 final int [] validStates = { -1 , 0 , 1 };
175113 final int givenState = 10 ;
176114 final String message = "State can't be %s, It can be one of %s." ;
177- try {
178- checkState (Arrays .binarySearch (validStates , givenState ) < 0 , message , givenState , Arrays .toString (validStates ));
179- } catch (Exception exception ) {
180- assertThat (exception ).isInstanceOf (IllegalStateException .class );
181- assertThat (exception ).hasNoCause ();
182- assertThat (exception ).hasMessage (message , givenState , Arrays .toString (validStates ));
183- }
184- }
185115
116+ assertThatThrownBy (() -> checkState (Arrays .binarySearch (validStates , givenState ) > 0 , message , givenState , Arrays .toString (validStates ))).isInstanceOf (IllegalStateException .class ).hasMessage (message , givenState , Arrays .toString (validStates ))
117+ .hasNoCause ();
118+ }
186119}
0 commit comments