Skip to content

Commit d76bba5

Browse files
committed
Migrate from ExpectedException rule to AssertJ
Replace ExpectedException JUnit rules with AssertJ exception assertions. Closes gh-14336
1 parent 42cb0ef commit d76bba5

File tree

273 files changed

+2752
-3624
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

273 files changed

+2752
-3624
lines changed

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/AuthorizationExceptionMatcher.java

Lines changed: 0 additions & 48 deletions
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,14 +16,15 @@
1616

1717
package org.springframework.boot.actuate.autoconfigure.cloudfoundry;
1818

19-
import org.junit.Rule;
19+
import java.util.function.Consumer;
20+
2021
import org.junit.Test;
21-
import org.junit.rules.ExpectedException;
2222

2323
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason;
2424
import org.springframework.util.Base64Utils;
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
27+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2728

2829
/**
2930
* Tests for {@link Token}.
@@ -32,43 +33,40 @@
3233
*/
3334
public class TokenTests {
3435

35-
@Rule
36-
public ExpectedException thrown = ExpectedException.none();
37-
3836
@Test
3937
public void invalidJwtShouldThrowException() {
40-
this.thrown
41-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
42-
new Token("invalid-token");
38+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
39+
.isThrownBy(() -> new Token("invalid-token"))
40+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
4341
}
4442

4543
@Test
4644
public void invalidJwtClaimsShouldThrowException() {
4745
String header = "{\"alg\": \"RS256\", \"kid\": \"key-id\", \"typ\": \"JWT\"}";
4846
String claims = "invalid-claims";
49-
this.thrown
50-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
51-
new Token(Base64Utils.encodeToString(header.getBytes()) + "."
52-
+ Base64Utils.encodeToString(claims.getBytes()));
47+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
48+
.isThrownBy(() -> new Token(Base64Utils.encodeToString(header.getBytes())
49+
+ "." + Base64Utils.encodeToString(claims.getBytes())))
50+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
5351
}
5452

5553
@Test
5654
public void invalidJwtHeaderShouldThrowException() {
5755
String header = "invalid-header";
5856
String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\"}";
59-
this.thrown
60-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
61-
new Token(Base64Utils.encodeToString(header.getBytes()) + "."
62-
+ Base64Utils.encodeToString(claims.getBytes()));
57+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
58+
.isThrownBy(() -> new Token(Base64Utils.encodeToString(header.getBytes())
59+
+ "." + Base64Utils.encodeToString(claims.getBytes())))
60+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
6361
}
6462

6563
@Test
6664
public void emptyJwtSignatureShouldThrowException() {
6765
String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJ0b3B0YWwu"
6866
+ "Y29tIiwiZXhwIjoxNDI2NDIwODAwLCJhd2Vzb21lIjp0cnVlfQ.";
69-
this.thrown
70-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
71-
new Token(token);
67+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
68+
.isThrownBy(() -> new Token(token))
69+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
7270
}
7371

7472
@Test
@@ -93,39 +91,39 @@ public void getSignatureAlgorithmWhenAlgIsNullShouldThrowException() {
9391
String header = "{\"kid\": \"key-id\", \"typ\": \"JWT\"}";
9492
String claims = "{\"exp\": 2147483647, \"iss\": \"http://localhost:8080/uaa/oauth/token\"}";
9593
Token token = createToken(header, claims);
96-
this.thrown
97-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
98-
token.getSignatureAlgorithm();
94+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
95+
.isThrownBy(() -> token.getSignatureAlgorithm())
96+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
9997
}
10098

10199
@Test
102100
public void getIssuerWhenIssIsNullShouldThrowException() {
103101
String header = "{\"alg\": \"RS256\", \"kid\": \"key-id\", \"typ\": \"JWT\"}";
104102
String claims = "{\"exp\": 2147483647}";
105103
Token token = createToken(header, claims);
106-
this.thrown
107-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
108-
token.getIssuer();
104+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
105+
.isThrownBy(() -> token.getIssuer())
106+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
109107
}
110108

111109
@Test
112110
public void getKidWhenKidIsNullShouldThrowException() {
113111
String header = "{\"alg\": \"RS256\", \"typ\": \"JWT\"}";
114112
String claims = "{\"exp\": 2147483647}";
115113
Token token = createToken(header, claims);
116-
this.thrown
117-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
118-
token.getKeyId();
114+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
115+
.isThrownBy(() -> token.getKeyId())
116+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
119117
}
120118

121119
@Test
122120
public void getExpiryWhenExpIsNullShouldThrowException() {
123121
String header = "{\"alg\": \"RS256\", \"kid\": \"key-id\", \"typ\": \"JWT\"}";
124122
String claims = "{\"iss\": \"http://localhost:8080/uaa/oauth/token\"" + "}";
125123
Token token = createToken(header, claims);
126-
this.thrown
127-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
128-
token.getExpiry();
124+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
125+
.isThrownBy(() -> token.getExpiry())
126+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
129127
}
130128

131129
private Token createToken(String header, String claims) {
@@ -135,4 +133,9 @@ private Token createToken(String header, String claims) {
135133
return token;
136134
}
137135

136+
private Consumer<CloudFoundryAuthorizationException> reasonRequirement(
137+
Reason reason) {
138+
return (ex) -> assertThat(ex.getReason()).isEqualTo(reason);
139+
}
140+
138141
}

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/reactive/ReactiveCloudFoundryActuatorAutoConfigurationTests.java

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,7 @@
2424
import javax.net.ssl.SSLException;
2525

2626
import org.junit.After;
27-
import org.junit.Rule;
2827
import org.junit.Test;
29-
import org.junit.rules.ExpectedException;
3028
import reactor.netty.http.HttpResources;
3129

3230
import org.springframework.boot.actuate.autoconfigure.endpoint.EndpointAutoConfiguration;
@@ -65,7 +63,7 @@
6563
import org.springframework.web.reactive.function.client.WebClient;
6664

6765
import static org.assertj.core.api.Assertions.assertThat;
68-
import static org.hamcrest.Matchers.instanceOf;
66+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
6967
import static org.mockito.Mockito.mock;
7068

7169
/**
@@ -75,9 +73,6 @@
7573
*/
7674
public class ReactiveCloudFoundryActuatorAutoConfigurationTests {
7775

78-
@Rule
79-
public ExpectedException thrown = ExpectedException.none();
80-
8176
private final ReactiveWebApplicationContextRunner contextRunner = new ReactiveWebApplicationContextRunner()
8277
.withConfiguration(AutoConfigurations.of(
8378
ReactiveSecurityAutoConfiguration.class,
@@ -335,9 +330,11 @@ public void sslValidationNotSkippedByDefault() {
335330
.getField(interceptor, "cloudFoundrySecurityService");
336331
WebClient webClient = (WebClient) ReflectionTestUtils
337332
.getField(interceptorSecurityService, "webClient");
338-
this.thrown.expectCause(instanceOf(SSLException.class));
339-
webClient.get().uri("https://self-signed.badssl.com/").exchange()
340-
.block();
333+
assertThatExceptionOfType(RuntimeException.class)
334+
.isThrownBy(
335+
webClient.get().uri("https://self-signed.badssl.com/")
336+
.exchange()::block)
337+
.withCauseInstanceOf(SSLException.class);
341338
});
342339
}
343340

spring-boot-project/spring-boot-actuator-autoconfigure/src/test/java/org/springframework/boot/actuate/autoconfigure/cloudfoundry/servlet/CloudFoundrySecurityServiceTests.java

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2017 the original author or authors.
2+
* Copyright 2012-2018 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -17,14 +17,13 @@
1717
package org.springframework.boot.actuate.autoconfigure.cloudfoundry.servlet;
1818

1919
import java.util.Map;
20+
import java.util.function.Consumer;
2021

2122
import org.junit.Before;
22-
import org.junit.Rule;
2323
import org.junit.Test;
24-
import org.junit.rules.ExpectedException;
2524

2625
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.AccessLevel;
27-
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.AuthorizationExceptionMatcher;
26+
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException;
2827
import org.springframework.boot.actuate.autoconfigure.cloudfoundry.CloudFoundryAuthorizationException.Reason;
2928
import org.springframework.boot.test.web.client.MockServerRestTemplateCustomizer;
3029
import org.springframework.boot.web.client.RestTemplateBuilder;
@@ -35,6 +34,7 @@
3534
import org.springframework.web.client.RestTemplate;
3635

3736
import static org.assertj.core.api.Assertions.assertThat;
37+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
3838
import static org.springframework.test.web.client.match.MockRestRequestMatchers.header;
3939
import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
4040
import static org.springframework.test.web.client.response.MockRestResponseCreators.withServerError;
@@ -49,9 +49,6 @@
4949
*/
5050
public class CloudFoundrySecurityServiceTests {
5151

52-
@Rule
53-
public ExpectedException thrown = ExpectedException.none();
54-
5552
private static final String CLOUD_CONTROLLER = "http://my-cloud-controller.com";
5653

5754
private static final String CLOUD_CONTROLLER_PERMISSIONS = CLOUD_CONTROLLER
@@ -123,29 +120,29 @@ public void getAccessLevelWhenTokenIsNotValidShouldThrowException() {
123120
this.server.expect(requestTo(CLOUD_CONTROLLER_PERMISSIONS))
124121
.andExpect(header("Authorization", "bearer my-access-token"))
125122
.andRespond(withUnauthorizedRequest());
126-
this.thrown
127-
.expect(AuthorizationExceptionMatcher.withReason(Reason.INVALID_TOKEN));
128-
this.securityService.getAccessLevel("my-access-token", "my-app-id");
123+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class).isThrownBy(
124+
() -> this.securityService.getAccessLevel("my-access-token", "my-app-id"))
125+
.satisfies(reasonRequirement(Reason.INVALID_TOKEN));
129126
}
130127

131128
@Test
132129
public void getAccessLevelWhenForbiddenShouldThrowException() {
133130
this.server.expect(requestTo(CLOUD_CONTROLLER_PERMISSIONS))
134131
.andExpect(header("Authorization", "bearer my-access-token"))
135132
.andRespond(withStatus(HttpStatus.FORBIDDEN));
136-
this.thrown
137-
.expect(AuthorizationExceptionMatcher.withReason(Reason.ACCESS_DENIED));
138-
this.securityService.getAccessLevel("my-access-token", "my-app-id");
133+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class).isThrownBy(
134+
() -> this.securityService.getAccessLevel("my-access-token", "my-app-id"))
135+
.satisfies(reasonRequirement(Reason.ACCESS_DENIED));
139136
}
140137

141138
@Test
142139
public void getAccessLevelWhenCloudControllerIsNotReachableThrowsException() {
143140
this.server.expect(requestTo(CLOUD_CONTROLLER_PERMISSIONS))
144141
.andExpect(header("Authorization", "bearer my-access-token"))
145142
.andRespond(withServerError());
146-
this.thrown.expect(
147-
AuthorizationExceptionMatcher.withReason(Reason.SERVICE_UNAVAILABLE));
148-
this.securityService.getAccessLevel("my-access-token", "my-app-id");
143+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class).isThrownBy(
144+
() -> this.securityService.getAccessLevel("my-access-token", "my-app-id"))
145+
.satisfies(reasonRequirement(Reason.SERVICE_UNAVAILABLE));
149146
}
150147

151148
@Test
@@ -188,9 +185,9 @@ public void fetchTokenKeysWhenUnsuccessfulShouldThrowException() {
188185
"{\"token_endpoint\":\"" + UAA_URL + "\"}", MediaType.APPLICATION_JSON));
189186
this.server.expect(requestTo(UAA_URL + "/token_keys"))
190187
.andRespond(withServerError());
191-
this.thrown.expect(
192-
AuthorizationExceptionMatcher.withReason(Reason.SERVICE_UNAVAILABLE));
193-
this.securityService.fetchTokenKeys();
188+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
189+
.isThrownBy(() -> this.securityService.fetchTokenKeys())
190+
.satisfies(reasonRequirement(Reason.SERVICE_UNAVAILABLE));
194191
}
195192

196193
@Test
@@ -209,9 +206,14 @@ public void getUaaUrlShouldCallCloudControllerInfoOnlyOnce() {
209206
public void getUaaUrlWhenCloudControllerUrlIsNotReachableShouldThrowException() {
210207
this.server.expect(requestTo(CLOUD_CONTROLLER + "/info"))
211208
.andRespond(withServerError());
212-
this.thrown.expect(
213-
AuthorizationExceptionMatcher.withReason(Reason.SERVICE_UNAVAILABLE));
214-
this.securityService.getUaaUrl();
209+
assertThatExceptionOfType(CloudFoundryAuthorizationException.class)
210+
.isThrownBy(() -> this.securityService.getUaaUrl())
211+
.satisfies(reasonRequirement(Reason.SERVICE_UNAVAILABLE));
212+
}
213+
214+
private Consumer<CloudFoundryAuthorizationException> reasonRequirement(
215+
Reason reason) {
216+
return (ex) -> assertThat(ex.getReason()).isEqualTo(reason);
215217
}
216218

217219
}

0 commit comments

Comments
 (0)