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

@@ -26,8 +26,8 @@ import org.springframework.security.crypto.codec.Hex;
import org.springframework.security.crypto.keygen.BytesKeyGenerator;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import static org.springframework.security.crypto.encrypt.AesBytesEncryptor.CipherAlgorithm.GCM;
import static org.springframework.security.crypto.encrypt.CipherUtils.newSecretKey;
import static org.springframework.security.crypto.password.Pbkdf2PasswordEncoder.SecretKeyFactoryAlgorithm.PBKDF2WithHmacSHA1;
@@ -48,8 +48,8 @@ public class AesBytesEncryptorTests {
@Before
public void setUp() {
this.generator = mock(BytesKeyGenerator.class);
when(this.generator.generateKey()).thenReturn(Hex.decode("4b0febebd439db7ca77153cb254520c3"));
when(this.generator.getKeyLength()).thenReturn(16);
given(this.generator.generateKey()).willReturn(Hex.decode("4b0febebd439db7ca77153cb254520c3"));
given(this.generator.getKeyLength()).willReturn(16);
}
@Test

View File

@@ -29,9 +29,9 @@ import org.mockito.junit.MockitoJUnitRunner;
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.verify;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
/**
* @author Rob Winch
@@ -101,14 +101,14 @@ public class DelegatingPasswordEncoderTests {
@Test
public void encodeWhenValidThenUsesIdForEncode() {
when(this.bcrypt.encode(this.rawPassword)).thenReturn(this.encodedPassword);
given(this.bcrypt.encode(this.rawPassword)).willReturn(this.encodedPassword);
assertThat(this.passwordEncoder.encode(this.rawPassword)).isEqualTo(this.bcryptEncodedPassword);
}
@Test
public void matchesWhenBCryptThenDelegatesToBCrypt() {
when(this.bcrypt.matches(this.rawPassword, this.encodedPassword)).thenReturn(true);
given(this.bcrypt.matches(this.rawPassword, this.encodedPassword)).willReturn(true);
assertThat(this.passwordEncoder.matches(this.rawPassword, this.bcryptEncodedPassword)).isTrue();
@@ -118,7 +118,7 @@ public class DelegatingPasswordEncoderTests {
@Test
public void matchesWhenNoopThenDelegatesToNoop() {
when(this.noop.matches(this.rawPassword, this.encodedPassword)).thenReturn(true);
given(this.noop.matches(this.rawPassword, this.encodedPassword)).willReturn(true);
assertThat(this.passwordEncoder.matches(this.rawPassword, this.noopEncodedPassword)).isTrue();
@@ -188,7 +188,7 @@ public class DelegatingPasswordEncoderTests {
public void matchesWhenNullIdThenDelegatesToInvalidId() {
this.delegates.put(null, this.invalidId);
this.passwordEncoder = new DelegatingPasswordEncoder(this.bcryptId, this.delegates);
when(this.invalidId.matches(this.rawPassword, this.encodedPassword)).thenReturn(true);
given(this.invalidId.matches(this.rawPassword, this.encodedPassword)).willReturn(true);
assertThat(this.passwordEncoder.matches(this.rawPassword, this.encodedPassword)).isTrue();
@@ -225,7 +225,7 @@ public class DelegatingPasswordEncoderTests {
@Test
public void upgradeEncodingWhenSameIdAndEncoderTrueThenEncoderDecidesTrue() {
when(this.bcrypt.upgradeEncoding(any())).thenReturn(true);
given(this.bcrypt.upgradeEncoding(any())).willReturn(true);
assertThat(this.passwordEncoder.upgradeEncoding(this.bcryptEncodedPassword)).isTrue();