Skip to content

Commit b702f5e

Browse files
committed
Replace use of ExpectedException rule with AssertJ
1 parent 2c73788 commit b702f5e

File tree

3 files changed

+26
-56
lines changed

3 files changed

+26
-56
lines changed

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/resourceserver/BearerTokenAuthenticationExceptionTests.java

+7-14
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package org.springframework.security.oauth2.resourceserver;
1818

19-
import org.junit.Rule;
2019
import org.junit.Test;
21-
import org.junit.rules.ExpectedException;
20+
2221
import org.springframework.http.HttpStatus;
2322

2423
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2525

2626
/**
2727
* Tests for {@link BearerTokenAuthenticationException}.
@@ -34,23 +34,18 @@ public class BearerTokenAuthenticationExceptionTests {
3434

3535
private static final String TEST_MESSAGE = "test-message";
3636

37-
@Rule
38-
public ExpectedException thrown = ExpectedException.none();
39-
4037
@Test
4138
public void constructorWithAllParametersWhenErrorIsValidThenCreated() {
4239
BearerTokenAuthenticationException exception = new BearerTokenAuthenticationException(TEST_ERROR, TEST_MESSAGE,
43-
new Throwable());
40+
new Throwable());
4441

4542
assertThat(exception.getError()).isEqualTo(TEST_ERROR);
4643
}
4744

4845
@Test
4946
public void constructorWithAllParametersWhenErrorIsNullThenThrowIllegalArgumentException() {
50-
this.thrown.expect(IllegalArgumentException.class);
51-
this.thrown.expectMessage("error must not be null");
52-
53-
new BearerTokenAuthenticationException(null, TEST_MESSAGE, new Throwable());
47+
assertThatThrownBy(() -> new BearerTokenAuthenticationException(null, TEST_MESSAGE, new Throwable()))
48+
.isInstanceOf(IllegalArgumentException.class).hasMessage("error must not be null");
5449
}
5550

5651
@Test
@@ -62,10 +57,8 @@ public void constructorWithErrorAndMessageWhenErrorIsValidThenCreated() {
6257

6358
@Test
6459
public void constructorWithErrorAndMessageWhenErrorIsNullThenThrowIllegalArgumentException() {
65-
this.thrown.expect(IllegalArgumentException.class);
66-
this.thrown.expectMessage("error must not be null");
67-
68-
new BearerTokenAuthenticationException(null, TEST_MESSAGE);
60+
assertThatThrownBy(() -> new BearerTokenAuthenticationException(null, TEST_MESSAGE))
61+
.isInstanceOf(IllegalArgumentException.class).hasMessage("error must not be null");
6962
}
7063

7164
}

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/resourceserver/BearerTokenErrorTests.java

+14-29
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,12 @@
1616

1717
package org.springframework.security.oauth2.resourceserver;
1818

19-
import org.junit.Rule;
2019
import org.junit.Test;
21-
import org.junit.rules.ExpectedException;
20+
2221
import org.springframework.http.HttpStatus;
2322

2423
import static org.assertj.core.api.Assertions.assertThat;
24+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2525

2626
/**
2727
* Tests for {@link BearerTokenError}.
@@ -40,9 +40,6 @@ public class BearerTokenErrorTests {
4040

4141
private static final String TEST_SCOPE = "test-scope";
4242

43-
@Rule
44-
public ExpectedException thrown = ExpectedException.none();
45-
4643
@Test
4744
public void constructorWithErrorCodeWhenErrorCodeIsValidThenCreated() {
4845
BearerTokenError error = new BearerTokenError(TEST_ERROR_CODE, TEST_HTTP_STATUS);
@@ -56,26 +53,20 @@ public void constructorWithErrorCodeWhenErrorCodeIsValidThenCreated() {
5653

5754
@Test
5855
public void constructorWithErrorCodeAndHttpStatusWhenErrorCodeIsNullThenThrowIllegalArgumentException() {
59-
this.thrown.expect(IllegalArgumentException.class);
60-
this.thrown.expectMessage("errorCode cannot be empty");
61-
62-
new BearerTokenError(null, TEST_HTTP_STATUS);
56+
assertThatThrownBy(() -> new BearerTokenError(null, TEST_HTTP_STATUS))
57+
.isInstanceOf(IllegalArgumentException.class).hasMessage("errorCode cannot be empty");
6358
}
6459

6560
@Test
6661
public void constructorWithErrorCodeAndHttpStatusWhenErrorCodeIsEmptyThenThrowIllegalArgumentException() {
67-
this.thrown.expect(IllegalArgumentException.class);
68-
this.thrown.expectMessage("errorCode cannot be empty");
69-
70-
new BearerTokenError("", TEST_HTTP_STATUS);
62+
assertThatThrownBy(() -> new BearerTokenError("", TEST_HTTP_STATUS))
63+
.isInstanceOf(IllegalArgumentException.class).hasMessage("errorCode cannot be empty");
7164
}
7265

7366
@Test
7467
public void constructorWithErrorCodeAndHttpStatusWhenHttpStatusIsNullThenThrowIllegalArgumentException() {
75-
this.thrown.expect(IllegalArgumentException.class);
76-
this.thrown.expectMessage("httpStatus must not be null");
77-
78-
new BearerTokenError(TEST_ERROR_CODE, null);
68+
assertThatThrownBy(() -> new BearerTokenError(TEST_ERROR_CODE, null))
69+
.isInstanceOf(IllegalArgumentException.class).hasMessage("httpStatus must not be null");
7970
}
8071

8172
@Test
@@ -92,26 +83,20 @@ public void constructorWithAllParametersWhenAllParametersAreValidThenCreated() {
9283

9384
@Test
9485
public void constructorWithAllParametersWhenErrorCodeIsNullThenThrowIllegalArgumentException() {
95-
this.thrown.expect(IllegalArgumentException.class);
96-
this.thrown.expectMessage("errorCode cannot be empty");
97-
98-
new BearerTokenError(null, TEST_HTTP_STATUS, TEST_DESCRIPTION, TEST_URI, TEST_SCOPE);
86+
assertThatThrownBy(() -> new BearerTokenError(null, TEST_HTTP_STATUS, TEST_DESCRIPTION, TEST_URI, TEST_SCOPE))
87+
.isInstanceOf(IllegalArgumentException.class).hasMessage("errorCode cannot be empty");
9988
}
10089

10190
@Test
10291
public void constructorWithAllParametersWhenErrorCodeIsEmptyThenThrowIllegalArgumentException() {
103-
this.thrown.expect(IllegalArgumentException.class);
104-
this.thrown.expectMessage("errorCode cannot be empty");
105-
106-
new BearerTokenError("", TEST_HTTP_STATUS, TEST_DESCRIPTION, TEST_URI, TEST_SCOPE);
92+
assertThatThrownBy(() -> new BearerTokenError("", TEST_HTTP_STATUS, TEST_DESCRIPTION, TEST_URI, TEST_SCOPE))
93+
.isInstanceOf(IllegalArgumentException.class).hasMessage("errorCode cannot be empty");
10794
}
10895

10996
@Test
11097
public void constructorWithAllParametersWhenHttpStatusIsNullThenThrowIllegalArgumentException() {
111-
this.thrown.expect(IllegalArgumentException.class);
112-
this.thrown.expectMessage("httpStatus must not be null");
113-
114-
new BearerTokenError(TEST_ERROR_CODE, null, TEST_DESCRIPTION, TEST_URI, TEST_SCOPE);
98+
assertThatThrownBy(() -> new BearerTokenError(TEST_ERROR_CODE, null, TEST_DESCRIPTION, TEST_URI, TEST_SCOPE))
99+
.isInstanceOf(IllegalArgumentException.class).hasMessage("httpStatus must not be null");
115100
}
116101

117102
}

oauth2/oauth2-resource-server/src/test/java/org/springframework/security/oauth2/resourceserver/web/DefaultBearerTokenResolverTests.java

+5-13
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,13 @@
1919
import java.util.Base64;
2020

2121
import org.junit.Before;
22-
import org.junit.Rule;
2322
import org.junit.Test;
24-
import org.junit.rules.ExpectedException;
2523

2624
import org.springframework.mock.web.MockHttpServletRequest;
2725
import org.springframework.security.oauth2.resourceserver.BearerTokenAuthenticationException;
2826

2927
import static org.assertj.core.api.Assertions.assertThat;
28+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
3029

3130
/**
3231
* Tests for {@link DefaultBearerTokenResolver}.
@@ -37,9 +36,6 @@ public class DefaultBearerTokenResolverTests {
3736

3837
private static final String TEST_TOKEN = "test-token";
3938

40-
@Rule
41-
public ExpectedException thrown = ExpectedException.none();
42-
4339
private DefaultBearerTokenResolver resolver;
4440

4541
@Before
@@ -72,29 +68,25 @@ public void resolveWhenHeaderWithWrongSchemeIsPresentThenTokenIsNotResolved() {
7268

7369
@Test
7470
public void resolveWhenValidHeaderIsPresentTogetherWithFormParameterThenAuthenticationExceptionIsThrown() {
75-
this.thrown.expect(BearerTokenAuthenticationException.class);
76-
this.thrown.expectMessage("[invalid_request]");
77-
7871
MockHttpServletRequest request = new MockHttpServletRequest();
7972
request.addHeader("Authorization", "Bearer " + TEST_TOKEN);
8073
request.setMethod("POST");
8174
request.setContentType("application/x-www-form-urlencoded");
8275
request.addParameter("access_token", TEST_TOKEN);
8376

84-
this.resolver.resolve(request);
77+
assertThatThrownBy(() -> this.resolver.resolve(request)).isInstanceOf(BearerTokenAuthenticationException.class)
78+
.hasMessageContaining("[invalid_request]");
8579
}
8680

8781
@Test
8882
public void resolveWhenValidHeaderIsPresentTogetherWithQueryParameterThenAuthenticationExceptionIsThrown() {
89-
this.thrown.expect(BearerTokenAuthenticationException.class);
90-
this.thrown.expectMessage("[invalid_request]");
91-
9283
MockHttpServletRequest request = new MockHttpServletRequest();
9384
request.addHeader("Authorization", "Bearer " + TEST_TOKEN);
9485
request.setMethod("GET");
9586
request.addParameter("access_token", TEST_TOKEN);
9687

97-
this.resolver.resolve(request);
88+
assertThatThrownBy(() -> this.resolver.resolve(request)).isInstanceOf(BearerTokenAuthenticationException.class)
89+
.hasMessageContaining("[invalid_request]");
9890
}
9991

10092
@Test

0 commit comments

Comments
 (0)