Skip to content

Commit 9a9bbac

Browse files
committed
Using Assert4J
1 parent f9c8af5 commit 9a9bbac

File tree

1 file changed

+30
-32
lines changed

1 file changed

+30
-32
lines changed

spring-cloud-config-client/src/test/java/org/springframework/cloud/config/client/ConfigServerConfigDataLoaderTests.java

+30-32
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727

2828
import org.apache.commons.logging.Log;
2929
import org.apache.commons.logging.LogFactory;
30-
import org.junit.jupiter.api.Assertions;
30+
import org.assertj.core.api.ThrowableAssertAlternative;
3131
import org.junit.jupiter.api.BeforeEach;
3232
import org.junit.jupiter.api.Disabled;
3333
import org.junit.jupiter.api.Test;
@@ -65,6 +65,7 @@
6565

6666
import static java.lang.String.format;
6767
import static org.assertj.core.api.Assertions.assertThat;
68+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6869
import static org.mockito.ArgumentMatchers.any;
6970
import static org.mockito.ArgumentMatchers.anyString;
7071
import static org.mockito.ArgumentMatchers.eq;
@@ -199,10 +200,10 @@ public void failFast() throws Exception {
199200
RestTemplate restTemplate = new RestTemplate(requestFactory);
200201
properties.setFailFast(true);
201202
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);
206207
}
207208

208209
@Test
@@ -213,20 +214,20 @@ public void failFastWhenNotFound() throws Exception {
213214
properties.setFailFast(true);
214215
properties.setLabel("WeSetUpToReturn_NOT_FOUND_ForThisLabel");
215216
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");
220221
}
221222

222223
@Test
223224
public void failFastWhenRequestTimesOut() {
224225
mockRequestTimedOut();
225226
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);
230231

231232
}
232233

@@ -239,10 +240,8 @@ public void failFastWhenBothPasswordAndAuthorizationPropertiesSet() throws Excep
239240
properties.setUsername("username");
240241
properties.setPassword("password");
241242
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");
246245
}
247246

248247
@Test
@@ -282,26 +281,24 @@ public void shouldThrowExceptionWhenPasswordAndAuthorizationBothSet() {
282281
properties.getHeaders().put(AUTHORIZATION, "Basic dXNlcm5hbWU6cGFzc3dvcmQNCg==");
283282
String username = "user";
284283
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'");
288287
}
289288

290289
@Test
291290
public void shouldThrowExceptionWhenNegativeReadTimeoutSet() {
292291
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.");
296294

297295
}
298296

299297
@Test
300298
public void shouldThrowExceptionWhenNegativeConnectTimeoutSet() {
301299
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.");
305302
}
306303

307304
@Test
@@ -376,8 +373,8 @@ public void shouldUseNextUriFor_NoExceptionNotOK_And_CONNECTION_TIMEOUT_ONLY_Str
376373
// is never tried, due to the strategy.
377374
assertNextUriIsNotTried(true, ConfigClientProperties.MultipleUriStrategy.CONNECTION_TIMEOUT_ONLY,
378375
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
381378
);
382379
}
383380

@@ -708,12 +705,13 @@ private void assertNextUriIsNotTried(boolean failFast,
708705
mockRequestResponse(requestFactory, goodURI, HttpStatus.OK);
709706
when(bootstrapContext.get(RestTemplate.class)).thenReturn(restTemplate);
710707

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");
713712
if (expectedCause != null) {
714-
assertThat(exception.getCause()).isInstanceOf(expectedCause);
713+
throwableAssertAlternative.withCauseInstanceOf(expectedCause);
715714
}
716-
assertThat(exception.getMessage()).contains("fail fast property is set");
717715
}
718716

719717
@SuppressWarnings("SameParameterValue")

0 commit comments

Comments
 (0)