27
27
28
28
import org .apache .commons .logging .Log ;
29
29
import org .apache .commons .logging .LogFactory ;
30
- import org .junit . jupiter .api .Assertions ;
30
+ import org .assertj . core .api .ThrowableAssertAlternative ;
31
31
import org .junit .jupiter .api .BeforeEach ;
32
32
import org .junit .jupiter .api .Disabled ;
33
33
import org .junit .jupiter .api .Test ;
65
65
66
66
import static java .lang .String .format ;
67
67
import static org .assertj .core .api .Assertions .assertThat ;
68
+ import static org .assertj .core .api .Assertions .assertThatExceptionOfType ;
68
69
import static org .mockito .ArgumentMatchers .any ;
69
70
import static org .mockito .ArgumentMatchers .anyString ;
70
71
import static org .mockito .ArgumentMatchers .eq ;
@@ -199,10 +200,10 @@ public void failFast() throws Exception {
199
200
RestTemplate restTemplate = new RestTemplate (requestFactory );
200
201
properties .setFailFast (true );
201
202
when (bootstrapContext .get (RestTemplate .class )).thenReturn (restTemplate );
202
- ConfigClientFailFastException exception = Assertions . assertThrows (ConfigClientFailFastException .class ,
203
- ( ) -> this .loader .load (context , resource ));
204
- assertThat ( exception . getCause ()). isInstanceOf ( HttpServerErrorException . class );
205
- assertThat ( exception . getMessage ()). contains ( "fail fast property is set" );
203
+ assertThatExceptionOfType (ConfigClientFailFastException .class )
204
+ . isThrownBy (( ) -> this .loader .load (context , resource ))
205
+ . withMessageContaining ( "fail fast property is set" )
206
+ . withCauseInstanceOf ( HttpServerErrorException . class );
206
207
}
207
208
208
209
@ Test
@@ -213,20 +214,20 @@ public void failFastWhenNotFound() throws Exception {
213
214
properties .setFailFast (true );
214
215
properties .setLabel ("WeSetUpToReturn_NOT_FOUND_ForThisLabel" );
215
216
when (bootstrapContext .get (RestTemplate .class )).thenReturn (restTemplate );
216
- ConfigClientFailFastException exception = Assertions . assertThrows (ConfigClientFailFastException .class ,
217
- ( ) -> this .loader .load (context , resource ));
218
- assertThat ( exception . getMessage ()). contains (
219
- "fail fast property is set, failing: None of labels [WeSetUpToReturn_NOT_FOUND_ForThisLabel] found" );
217
+ assertThatExceptionOfType (ConfigClientFailFastException .class )
218
+ . isThrownBy (( ) -> this .loader .load (context , resource ))
219
+ . withMessageContaining (
220
+ "fail fast property is set, failing: None of labels [WeSetUpToReturn_NOT_FOUND_ForThisLabel] found" );
220
221
}
221
222
222
223
@ Test
223
224
public void failFastWhenRequestTimesOut () {
224
225
mockRequestTimedOut ();
225
226
properties .setFailFast (true );
226
- ConfigClientFailFastException exception = Assertions . assertThrows (ConfigClientFailFastException .class ,
227
- ( ) -> this .loader .load (context , resource ));
228
- assertThat ( exception . getCause ()). isExactlyInstanceOf ( ResourceAccessException . class );
229
- assertThat ( exception . getMessage ()). contains ( "fail fast property is set" );
227
+ assertThatExceptionOfType (ConfigClientFailFastException .class )
228
+ . isThrownBy (( ) -> this .loader .load (context , resource ))
229
+ . withMessageContaining ( "fail fast property is set" )
230
+ . withCauseInstanceOf ( ResourceAccessException . class );
230
231
231
232
}
232
233
@@ -239,10 +240,8 @@ public void failFastWhenBothPasswordAndAuthorizationPropertiesSet() throws Excep
239
240
properties .setUsername ("username" );
240
241
properties .setPassword ("password" );
241
242
properties .getHeaders ().put (AUTHORIZATION , "Basic dXNlcm5hbWU6cGFzc3dvcmQNCg==" );
242
- IllegalStateException exception = Assertions .assertThrows (IllegalStateException .class ,
243
- () -> this .loader .load (context , resource ));
244
- assertThat (exception .getMessage ())
245
- .contains ("Could not locate PropertySource and the fail fast property is set, failing" );
243
+ assertThatExceptionOfType (IllegalStateException .class ).isThrownBy (() -> this .loader .load (context , resource ))
244
+ .withMessageContaining ("Could not locate PropertySource and the fail fast property is set, failing" );
246
245
}
247
246
248
247
@ Test
@@ -282,26 +281,24 @@ public void shouldThrowExceptionWhenPasswordAndAuthorizationBothSet() {
282
281
properties .getHeaders ().put (AUTHORIZATION , "Basic dXNlcm5hbWU6cGFzc3dvcmQNCg==" );
283
282
String username = "user" ;
284
283
String password = "pass" ;
285
- IllegalStateException exception = Assertions . assertThrows (IllegalStateException .class ,
286
- ( ) -> factory (properties ).addAuthorizationToken (headers , username , password ));
287
- assertThat ( exception . getMessage ()). contains ("You must set either 'password' or 'authorization'" );
284
+ assertThatExceptionOfType (IllegalStateException .class )
285
+ . isThrownBy (( ) -> factory (properties ).addAuthorizationToken (headers , username , password ))
286
+ . withMessageContaining ("You must set either 'password' or 'authorization'" );
288
287
}
289
288
290
289
@ Test
291
290
public void shouldThrowExceptionWhenNegativeReadTimeoutSet () {
292
291
properties .setRequestReadTimeout (-1 );
293
- IllegalStateException exception = Assertions .assertThrows (IllegalStateException .class ,
294
- () -> factory (properties ).create ());
295
- assertThat (exception .getMessage ()).contains ("Invalid Value for Read Timeout set." );
292
+ assertThatExceptionOfType (IllegalStateException .class ).isThrownBy (() -> factory (properties ).create ())
293
+ .withMessageContaining ("Invalid Value for Read Timeout set." );
296
294
297
295
}
298
296
299
297
@ Test
300
298
public void shouldThrowExceptionWhenNegativeConnectTimeoutSet () {
301
299
properties .setRequestConnectTimeout (-1 );
302
- IllegalStateException exception = Assertions .assertThrows (IllegalStateException .class ,
303
- () -> factory (properties ).create ());
304
- assertThat (exception .getMessage ()).contains ("Invalid Value for Connect Timeout set." );
300
+ assertThatExceptionOfType (IllegalStateException .class ).isThrownBy (() -> factory (properties ).create ())
301
+ .withMessageContaining ("Invalid Value for Connect Timeout set." );
305
302
}
306
303
307
304
@ Test
@@ -376,8 +373,8 @@ public void shouldUseNextUriFor_NoExceptionNotOK_And_CONNECTION_TIMEOUT_ONLY_Str
376
373
// is never tried, due to the strategy.
377
374
assertNextUriIsNotTried (true , ConfigClientProperties .MultipleUriStrategy .CONNECTION_TIMEOUT_ONLY ,
378
375
HttpStatus .TEMPORARY_REDIRECT , null // IllegalStateException has no cause,
379
- // because getRemoteEnvironment did
380
- // not throw an exception
376
+ // because getRemoteEnvironment did
377
+ // not throw an exception
381
378
);
382
379
}
383
380
@@ -708,12 +705,13 @@ private void assertNextUriIsNotTried(boolean failFast,
708
705
mockRequestResponse (requestFactory , goodURI , HttpStatus .OK );
709
706
when (bootstrapContext .get (RestTemplate .class )).thenReturn (restTemplate );
710
707
711
- ConfigClientFailFastException exception = Assertions .assertThrows (ConfigClientFailFastException .class ,
712
- () -> this .loader .load (context , resource ));
708
+ ThrowableAssertAlternative <ConfigClientFailFastException > throwableAssertAlternative = assertThatExceptionOfType (
709
+ ConfigClientFailFastException .class )
710
+ .isThrownBy (() -> this .loader .load (context , resource ))
711
+ .withMessageContaining ("fail fast property is set" );
713
712
if (expectedCause != null ) {
714
- assertThat ( exception . getCause ()). isInstanceOf (expectedCause );
713
+ throwableAssertAlternative . withCauseInstanceOf (expectedCause );
715
714
}
716
- assertThat (exception .getMessage ()).contains ("fail fast property is set" );
717
715
}
718
716
719
717
@ SuppressWarnings ("SameParameterValue" )
0 commit comments