Migrate to BDD Mockito

Migrate Mockito imports to use the BDD variant. This aligns better with
the "given" / "when" / "then" style used in most tests since the "given"
block now uses Mockito `given(...)` calls.

The commit also updates a few tests that were accidentally using
Power Mockito when regular Mockito could be used.

Issue gh-8945
This commit is contained in:
Phillip Webb
2020-07-27 12:53:19 -07:00
committed by Rob Winch
parent c12ced6aaa
commit db55ef4b3b
259 changed files with 2126 additions and 2125 deletions

View File

@@ -23,10 +23,10 @@ import org.junit.Test;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* Tests for verifying {@link DelegatingOAuth2TokenValidator}
@@ -50,8 +50,8 @@ public class DelegatingOAuth2TokenValidatorTests {
OAuth2TokenValidator<AbstractOAuth2Token> success = mock(OAuth2TokenValidator.class);
OAuth2TokenValidator<AbstractOAuth2Token> failure = mock(OAuth2TokenValidator.class);
when(success.validate(any(AbstractOAuth2Token.class))).thenReturn(OAuth2TokenValidatorResult.success());
when(failure.validate(any(AbstractOAuth2Token.class))).thenReturn(OAuth2TokenValidatorResult.failure(DETAIL));
given(success.validate(any(AbstractOAuth2Token.class))).willReturn(OAuth2TokenValidatorResult.success());
given(failure.validate(any(AbstractOAuth2Token.class))).willReturn(OAuth2TokenValidatorResult.failure(DETAIL));
DelegatingOAuth2TokenValidator<AbstractOAuth2Token> tokenValidator = new DelegatingOAuth2TokenValidator<>(
Arrays.asList(success, failure));
@@ -70,10 +70,10 @@ public class DelegatingOAuth2TokenValidatorTests {
OAuth2Error otherDetail = new OAuth2Error("another-error");
when(firstFailure.validate(any(AbstractOAuth2Token.class)))
.thenReturn(OAuth2TokenValidatorResult.failure(DETAIL));
when(secondFailure.validate(any(AbstractOAuth2Token.class)))
.thenReturn(OAuth2TokenValidatorResult.failure(otherDetail));
given(firstFailure.validate(any(AbstractOAuth2Token.class)))
.willReturn(OAuth2TokenValidatorResult.failure(DETAIL));
given(secondFailure.validate(any(AbstractOAuth2Token.class)))
.willReturn(OAuth2TokenValidatorResult.failure(otherDetail));
DelegatingOAuth2TokenValidator<AbstractOAuth2Token> tokenValidator = new DelegatingOAuth2TokenValidator<>(
firstFailure, secondFailure);
@@ -90,8 +90,8 @@ public class DelegatingOAuth2TokenValidatorTests {
OAuth2TokenValidator<AbstractOAuth2Token> firstSuccess = mock(OAuth2TokenValidator.class);
OAuth2TokenValidator<AbstractOAuth2Token> secondSuccess = mock(OAuth2TokenValidator.class);
when(firstSuccess.validate(any(AbstractOAuth2Token.class))).thenReturn(OAuth2TokenValidatorResult.success());
when(secondSuccess.validate(any(AbstractOAuth2Token.class))).thenReturn(OAuth2TokenValidatorResult.success());
given(firstSuccess.validate(any(AbstractOAuth2Token.class))).willReturn(OAuth2TokenValidatorResult.success());
given(secondSuccess.validate(any(AbstractOAuth2Token.class))).willReturn(OAuth2TokenValidatorResult.success());
DelegatingOAuth2TokenValidator<AbstractOAuth2Token> tokenValidator = new DelegatingOAuth2TokenValidator<>(
Arrays.asList(firstSuccess, secondSuccess));
@@ -115,8 +115,8 @@ public class DelegatingOAuth2TokenValidatorTests {
OAuth2TokenValidator<AbstractOAuth2Token> firstSuccess = mock(OAuth2TokenValidator.class);
OAuth2TokenValidator<AbstractOAuth2Token> secondSuccess = mock(OAuth2TokenValidator.class);
when(firstSuccess.validate(any(AbstractOAuth2Token.class))).thenReturn(OAuth2TokenValidatorResult.success());
when(secondSuccess.validate(any(AbstractOAuth2Token.class))).thenReturn(OAuth2TokenValidatorResult.success());
given(firstSuccess.validate(any(AbstractOAuth2Token.class))).willReturn(OAuth2TokenValidatorResult.success());
given(secondSuccess.validate(any(AbstractOAuth2Token.class))).willReturn(OAuth2TokenValidatorResult.success());
DelegatingOAuth2TokenValidator<AbstractOAuth2Token> firstValidator = new DelegatingOAuth2TokenValidator<>(
Arrays.asList(firstSuccess, secondSuccess));

View File

@@ -38,8 +38,8 @@ import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.assertj.core.api.Assertions.entry;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests for {@link OAuth2AccessTokenResponseHttpMessageConverter}.
@@ -146,7 +146,7 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests {
@Test
public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() {
Converter tokenResponseConverter = mock(Converter.class);
when(tokenResponseConverter.convert(any())).thenThrow(RuntimeException.class);
given(tokenResponseConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setTokenResponseConverter(tokenResponseConverter);
String tokenResponse = "{}";
@@ -186,7 +186,7 @@ public class OAuth2AccessTokenResponseHttpMessageConverterTests {
@Test
public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() {
Converter tokenResponseParametersConverter = mock(Converter.class);
when(tokenResponseParametersConverter.convert(any())).thenThrow(RuntimeException.class);
given(tokenResponseParametersConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setTokenResponseParametersConverter(tokenResponseParametersConverter);
OAuth2AccessTokenResponse accessTokenResponse = OAuth2AccessTokenResponse.withToken("access-token-1234")

View File

@@ -29,8 +29,8 @@ import org.springframework.security.oauth2.core.OAuth2Error;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
/**
* Tests for {@link OAuth2ErrorHttpMessageConverter}.
@@ -95,7 +95,7 @@ public class OAuth2ErrorHttpMessageConverterTests {
@Test
public void readInternalWhenConversionFailsThenThrowHttpMessageNotReadableException() {
Converter errorConverter = mock(Converter.class);
when(errorConverter.convert(any())).thenThrow(RuntimeException.class);
given(errorConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setErrorConverter(errorConverter);
String errorResponse = "{}";
@@ -124,7 +124,7 @@ public class OAuth2ErrorHttpMessageConverterTests {
@Test
public void writeInternalWhenConversionFailsThenThrowHttpMessageNotWritableException() {
Converter errorParametersConverter = mock(Converter.class);
when(errorParametersConverter.convert(any())).thenThrow(RuntimeException.class);
given(errorParametersConverter.convert(any())).willThrow(RuntimeException.class);
this.messageConverter.setErrorParametersConverter(errorParametersConverter);
OAuth2Error oauth2Error = new OAuth2Error("unauthorized_client", "The client is not authorized",