Skip to content

Commit db55ef4

Browse files
philwebbrwinch
authored andcommitted
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
1 parent c12ced6 commit db55ef4

259 files changed

Lines changed: 2126 additions & 2125 deletions

File tree

Some content is hidden

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

‎acl/src/test/java/org/springframework/security/acls/AclPermissionCacheOptimizerTests.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,10 @@
3030

3131
import static org.mockito.ArgumentMatchers.any;
3232
import static org.mockito.ArgumentMatchers.eq;
33+
import static org.mockito.BDDMockito.given;
3334
import static org.mockito.Mockito.mock;
3435
import static org.mockito.Mockito.verify;
3536
import static org.mockito.Mockito.verifyZeroInteractions;
36-
import static org.mockito.Mockito.when;
3737

3838
/**
3939
* @author Luke Taylor
@@ -51,8 +51,8 @@ public void eagerlyLoadsRequiredAcls() {
5151
pco.setSidRetrievalStrategy(sidStrat);
5252
Object[] dos = { new Object(), null, new Object() };
5353
ObjectIdentity[] oids = { new ObjectIdentityImpl("A", "1"), new ObjectIdentityImpl("A", "2") };
54-
when(oidStrat.getObjectIdentity(dos[0])).thenReturn(oids[0]);
55-
when(oidStrat.getObjectIdentity(dos[2])).thenReturn(oids[1]);
54+
given(oidStrat.getObjectIdentity(dos[0])).willReturn(oids[0]);
55+
given(oidStrat.getObjectIdentity(dos[2])).willReturn(oids[1]);
5656

5757
pco.cachePermissionsFor(mock(Authentication.class), Arrays.asList(dos));
5858

‎acl/src/test/java/org/springframework/security/acls/AclPermissionEvaluatorTests.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@
3030
import static org.mockito.ArgumentMatchers.any;
3131
import static org.mockito.ArgumentMatchers.anyList;
3232
import static org.mockito.ArgumentMatchers.eq;
33+
import static org.mockito.BDDMockito.given;
3334
import static org.mockito.Mockito.mock;
34-
import static org.mockito.Mockito.when;
3535

3636
/**
3737
* @author Luke Taylor
@@ -45,13 +45,13 @@ public void hasPermissionReturnsTrueIfAclGrantsPermission() {
4545
AclPermissionEvaluator pe = new AclPermissionEvaluator(service);
4646
ObjectIdentity oid = mock(ObjectIdentity.class);
4747
ObjectIdentityRetrievalStrategy oidStrategy = mock(ObjectIdentityRetrievalStrategy.class);
48-
when(oidStrategy.getObjectIdentity(any(Object.class))).thenReturn(oid);
48+
given(oidStrategy.getObjectIdentity(any(Object.class))).willReturn(oid);
4949
pe.setObjectIdentityRetrievalStrategy(oidStrategy);
5050
pe.setSidRetrievalStrategy(mock(SidRetrievalStrategy.class));
5151
Acl acl = mock(Acl.class);
5252

53-
when(service.readAclById(any(ObjectIdentity.class), anyList())).thenReturn(acl);
54-
when(acl.isGranted(anyList(), anyList(), eq(false))).thenReturn(true);
53+
given(service.readAclById(any(ObjectIdentity.class), anyList())).willReturn(acl);
54+
given(acl.isGranted(anyList(), anyList(), eq(false))).willReturn(true);
5555

5656
assertThat(pe.hasPermission(mock(Authentication.class), new Object(), "READ")).isTrue();
5757
}
@@ -65,13 +65,13 @@ public void resolvePermissionNonEnglishLocale() {
6565
AclPermissionEvaluator pe = new AclPermissionEvaluator(service);
6666
ObjectIdentity oid = mock(ObjectIdentity.class);
6767
ObjectIdentityRetrievalStrategy oidStrategy = mock(ObjectIdentityRetrievalStrategy.class);
68-
when(oidStrategy.getObjectIdentity(any(Object.class))).thenReturn(oid);
68+
given(oidStrategy.getObjectIdentity(any(Object.class))).willReturn(oid);
6969
pe.setObjectIdentityRetrievalStrategy(oidStrategy);
7070
pe.setSidRetrievalStrategy(mock(SidRetrievalStrategy.class));
7171
Acl acl = mock(Acl.class);
7272

73-
when(service.readAclById(any(ObjectIdentity.class), anyList())).thenReturn(acl);
74-
when(acl.isGranted(anyList(), anyList(), eq(false))).thenReturn(true);
73+
given(service.readAclById(any(ObjectIdentity.class), anyList())).willReturn(acl);
74+
given(acl.isGranted(anyList(), anyList(), eq(false))).willReturn(true);
7575

7676
assertThat(pe.hasPermission(mock(Authentication.class), new Object(), "write")).isTrue();
7777

‎acl/src/test/java/org/springframework/security/acls/afterinvocation/AclEntryAfterInvocationCollectionFilteringProviderTests.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -35,10 +35,10 @@
3535
import static org.assertj.core.api.Assertions.assertThat;
3636
import static org.mockito.ArgumentMatchers.any;
3737
import static org.mockito.ArgumentMatchers.anyBoolean;
38+
import static org.mockito.BDDMockito.given;
3839
import static org.mockito.Mockito.mock;
3940
import static org.mockito.Mockito.never;
4041
import static org.mockito.Mockito.verify;
41-
import static org.mockito.Mockito.when;
4242

4343
/**
4444
* @author Luke Taylor
@@ -50,8 +50,8 @@ public class AclEntryAfterInvocationCollectionFilteringProviderTests {
5050
public void objectsAreRemovedIfPermissionDenied() {
5151
AclService service = mock(AclService.class);
5252
Acl acl = mock(Acl.class);
53-
when(acl.isGranted(any(), any(), anyBoolean())).thenReturn(false);
54-
when(service.readAclById(any(), any())).thenReturn(acl);
53+
given(acl.isGranted(any(), any(), anyBoolean())).willReturn(false);
54+
given(service.readAclById(any(), any())).willReturn(acl);
5555
AclEntryAfterInvocationCollectionFilteringProvider provider = new AclEntryAfterInvocationCollectionFilteringProvider(
5656
service, Arrays.asList(mock(Permission.class)));
5757
provider.setObjectIdentityRetrievalStrategy(mock(ObjectIdentityRetrievalStrategy.class));

‎acl/src/test/java/org/springframework/security/acls/afterinvocation/AclEntryAfterInvocationProviderTests.java‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,10 @@
3838
import static org.assertj.core.api.Assertions.fail;
3939
import static org.mockito.ArgumentMatchers.any;
4040
import static org.mockito.ArgumentMatchers.anyBoolean;
41+
import static org.mockito.BDDMockito.given;
4142
import static org.mockito.Mockito.mock;
4243
import static org.mockito.Mockito.never;
4344
import static org.mockito.Mockito.verify;
44-
import static org.mockito.Mockito.when;
4545

4646
/**
4747
* @author Luke Taylor
@@ -64,8 +64,8 @@ public void rejectsMissingPermissions() {
6464
public void accessIsAllowedIfPermissionIsGranted() {
6565
AclService service = mock(AclService.class);
6666
Acl acl = mock(Acl.class);
67-
when(acl.isGranted(any(List.class), any(List.class), anyBoolean())).thenReturn(true);
68-
when(service.readAclById(any(), any())).thenReturn(acl);
67+
given(acl.isGranted(any(List.class), any(List.class), anyBoolean())).willReturn(true);
68+
given(service.readAclById(any(), any())).willReturn(acl);
6969
AclEntryAfterInvocationProvider provider = new AclEntryAfterInvocationProvider(service,
7070
Arrays.asList(mock(Permission.class)));
7171
provider.setMessageSource(new SpringSecurityMessageSource());
@@ -104,10 +104,10 @@ public void accessIsGrantedIfObjectTypeNotSupported() {
104104
public void accessIsDeniedIfPermissionIsNotGranted() {
105105
AclService service = mock(AclService.class);
106106
Acl acl = mock(Acl.class);
107-
when(acl.isGranted(any(List.class), any(List.class), anyBoolean())).thenReturn(false);
107+
given(acl.isGranted(any(List.class), any(List.class), anyBoolean())).willReturn(false);
108108
// Try a second time with no permissions found
109-
when(acl.isGranted(any(), any(List.class), anyBoolean())).thenThrow(new NotFoundException(""));
110-
when(service.readAclById(any(), any())).thenReturn(acl);
109+
given(acl.isGranted(any(), any(List.class), anyBoolean())).willThrow(new NotFoundException(""));
110+
given(service.readAclById(any(), any())).willReturn(acl);
111111
AclEntryAfterInvocationProvider provider = new AclEntryAfterInvocationProvider(service,
112112
Arrays.asList(mock(Permission.class)));
113113
provider.setProcessConfigAttribute("MY_ATTRIBUTE");

‎acl/src/test/java/org/springframework/security/acls/domain/AccessControlImplEntryTests.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525

2626
import static org.assertj.core.api.Assertions.assertThat;
2727
import static org.assertj.core.api.Assertions.fail;
28+
import static org.mockito.BDDMockito.given;
2829
import static org.mockito.Mockito.mock;
29-
import static org.mockito.Mockito.when;
3030

3131
/**
3232
* Tests for {@link AccessControlEntryImpl}.
@@ -87,7 +87,7 @@ public void testEquals() {
8787
final Acl mockAcl = mock(Acl.class);
8888
final ObjectIdentity oid = mock(ObjectIdentity.class);
8989

90-
when(mockAcl.getObjectIdentity()).thenReturn(oid);
90+
given(mockAcl.getObjectIdentity()).willReturn(oid);
9191
Sid sid = new PrincipalSid("johndoe");
9292

9393
AccessControlEntry ace = new AccessControlEntryImpl(1L, mockAcl, sid, BasePermission.ADMINISTRATION, true, true,

‎acl/src/test/java/org/springframework/security/acls/domain/AuditLoggerTests.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@
2626
import org.springframework.security.acls.model.AuditableAccessControlEntry;
2727

2828
import static org.assertj.core.api.Assertions.assertThat;
29+
import static org.mockito.BDDMockito.given;
2930
import static org.mockito.Mockito.mock;
30-
import static org.mockito.Mockito.when;
3131

3232
/**
3333
* Test class for {@link ConsoleAuditLogger}.
@@ -67,29 +67,29 @@ public void nonAuditableAceIsIgnored() {
6767

6868
@Test
6969
public void successIsNotLoggedIfAceDoesntRequireSuccessAudit() {
70-
when(this.ace.isAuditSuccess()).thenReturn(false);
70+
given(this.ace.isAuditSuccess()).willReturn(false);
7171
this.logger.logIfNeeded(true, this.ace);
7272
assertThat(this.bytes.size()).isZero();
7373
}
7474

7575
@Test
7676
public void successIsLoggedIfAceRequiresSuccessAudit() {
77-
when(this.ace.isAuditSuccess()).thenReturn(true);
77+
given(this.ace.isAuditSuccess()).willReturn(true);
7878

7979
this.logger.logIfNeeded(true, this.ace);
8080
assertThat(this.bytes.toString()).startsWith("GRANTED due to ACE");
8181
}
8282

8383
@Test
8484
public void failureIsntLoggedIfAceDoesntRequireFailureAudit() {
85-
when(this.ace.isAuditFailure()).thenReturn(false);
85+
given(this.ace.isAuditFailure()).willReturn(false);
8686
this.logger.logIfNeeded(false, this.ace);
8787
assertThat(this.bytes.size()).isZero();
8888
}
8989

9090
@Test
9191
public void failureIsLoggedIfAceRequiresFailureAudit() {
92-
when(this.ace.isAuditFailure()).thenReturn(true);
92+
given(this.ace.isAuditFailure()).willReturn(true);
9393
this.logger.logIfNeeded(false, this.ace);
9494
assertThat(this.bytes.toString()).startsWith("DENIED due to ACE");
9595
}

‎acl/src/test/java/org/springframework/security/acls/jdbc/EhCacheBasedAclCacheTests.java‎

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,9 @@
5252

5353
import static org.assertj.core.api.Assertions.assertThat;
5454
import static org.assertj.core.api.Assertions.fail;
55+
import static org.mockito.BDDMockito.given;
5556
import static org.mockito.Mockito.times;
5657
import static org.mockito.Mockito.verify;
57-
import static org.mockito.Mockito.when;
5858

5959
/**
6060
* Tests {@link EhCacheBasedAclCache}
@@ -220,14 +220,14 @@ public void putInCacheAclWithParent() {
220220

221221
@Test
222222
public void getFromCacheSerializable() {
223-
when(this.cache.get(this.acl.getId())).thenReturn(new Element(this.acl.getId(), this.acl));
223+
given(this.cache.get(this.acl.getId())).willReturn(new Element(this.acl.getId(), this.acl));
224224

225225
assertThat(this.myCache.getFromCache(this.acl.getId())).isEqualTo(this.acl);
226226
}
227227

228228
@Test
229229
public void getFromCacheSerializablePopulatesTransient() {
230-
when(this.cache.get(this.acl.getId())).thenReturn(new Element(this.acl.getId(), this.acl));
230+
given(this.cache.get(this.acl.getId())).willReturn(new Element(this.acl.getId(), this.acl));
231231

232232
this.myCache.putInCache(this.acl);
233233

@@ -242,14 +242,14 @@ public void getFromCacheSerializablePopulatesTransient() {
242242

243243
@Test
244244
public void getFromCacheObjectIdentity() {
245-
when(this.cache.get(this.acl.getId())).thenReturn(new Element(this.acl.getId(), this.acl));
245+
given(this.cache.get(this.acl.getId())).willReturn(new Element(this.acl.getId(), this.acl));
246246

247247
assertThat(this.myCache.getFromCache(this.acl.getId())).isEqualTo(this.acl);
248248
}
249249

250250
@Test
251251
public void getFromCacheObjectIdentityPopulatesTransient() {
252-
when(this.cache.get(this.acl.getObjectIdentity())).thenReturn(new Element(this.acl.getId(), this.acl));
252+
given(this.cache.get(this.acl.getObjectIdentity())).willReturn(new Element(this.acl.getId(), this.acl));
253253

254254
this.myCache.putInCache(this.acl);
255255

@@ -264,7 +264,7 @@ public void getFromCacheObjectIdentityPopulatesTransient() {
264264

265265
@Test
266266
public void evictCacheSerializable() {
267-
when(this.cache.get(this.acl.getObjectIdentity())).thenReturn(new Element(this.acl.getId(), this.acl));
267+
given(this.cache.get(this.acl.getObjectIdentity())).willReturn(new Element(this.acl.getId(), this.acl));
268268

269269
this.myCache.evictFromCache(this.acl.getObjectIdentity());
270270

@@ -274,7 +274,7 @@ public void evictCacheSerializable() {
274274

275275
@Test
276276
public void evictCacheObjectIdentity() {
277-
when(this.cache.get(this.acl.getId())).thenReturn(new Element(this.acl.getId(), this.acl));
277+
given(this.cache.get(this.acl.getId())).willReturn(new Element(this.acl.getId(), this.acl));
278278

279279
this.myCache.evictFromCache(this.acl.getId());
280280

‎acl/src/test/java/org/springframework/security/acls/jdbc/JdbcAclServiceTests.java‎

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@
4747
import static org.mockito.ArgumentMatchers.any;
4848
import static org.mockito.ArgumentMatchers.anyList;
4949
import static org.mockito.ArgumentMatchers.anyString;
50-
import static org.mockito.Mockito.when;
50+
import static org.mockito.BDDMockito.given;
5151

5252
/**
5353
* Unit and Integration tests the ACL JdbcAclService using an in-memory database.
@@ -93,7 +93,7 @@ public void tearDownEmbeddedDatabase() {
9393
@Test(expected = NotFoundException.class)
9494
public void readAclByIdMissingAcl() {
9595
Map<ObjectIdentity, Acl> result = new HashMap<>();
96-
when(this.lookupStrategy.readAclsById(anyList(), anyList())).thenReturn(result);
96+
given(this.lookupStrategy.readAclsById(anyList(), anyList())).willReturn(result);
9797
ObjectIdentity objectIdentity = new ObjectIdentityImpl(Object.class, 1);
9898
List<Sid> sids = Arrays.<Sid>asList(new PrincipalSid("user"));
9999

@@ -105,7 +105,7 @@ public void findOneChildren() {
105105
List<ObjectIdentity> result = new ArrayList<>();
106106
result.add(new ObjectIdentityImpl(Object.class, "5577"));
107107
Object[] args = { "1", "org.springframework.security.acls.jdbc.JdbcAclServiceTests$MockLongIdDomainObject" };
108-
when(this.jdbcOperations.query(anyString(), aryEq(args), any(RowMapper.class))).thenReturn(result);
108+
given(this.jdbcOperations.query(anyString(), aryEq(args), any(RowMapper.class))).willReturn(result);
109109
ObjectIdentity objectIdentity = new ObjectIdentityImpl(MockLongIdDomainObject.class, 1L);
110110

111111
List<ObjectIdentity> objectIdentities = this.aclService.findChildren(objectIdentity);

‎acl/src/test/java/org/springframework/security/acls/jdbc/JdbcMutableAclServiceTests.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@
5555

5656
import static org.assertj.core.api.Assertions.assertThat;
5757
import static org.assertj.core.api.Assertions.fail;
58+
import static org.mockito.BDDMockito.given;
5859
import static org.mockito.Mockito.spy;
59-
import static org.mockito.Mockito.when;
6060

6161
/**
6262
* Integration tests the ACL system using an in-memory database.
@@ -537,7 +537,7 @@ public void testProcessingCustomSid() {
537537
CustomJdbcMutableAclService customJdbcMutableAclService = spy(
538538
new CustomJdbcMutableAclService(this.dataSource, this.lookupStrategy, this.aclCache));
539539
CustomSid customSid = new CustomSid("Custom sid");
540-
when(customJdbcMutableAclService.createOrRetrieveSidPrimaryKey("Custom sid", false, false)).thenReturn(1L);
540+
given(customJdbcMutableAclService.createOrRetrieveSidPrimaryKey("Custom sid", false, false)).willReturn(1L);
541541

542542
Long result = customJdbcMutableAclService.createOrRetrieveSidPrimaryKey(customSid, false);
543543

‎acl/src/test/java/org/springframework/security/acls/sid/SidRetrievalStrategyTests.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,8 @@
3131

3232
import static org.assertj.core.api.Assertions.assertThat;
3333
import static org.mockito.ArgumentMatchers.anyCollection;
34+
import static org.mockito.BDDMockito.given;
3435
import static org.mockito.Mockito.mock;
35-
import static org.mockito.Mockito.when;
3636

3737
/**
3838
* Tests for {@link SidRetrievalStrategyImpl}
@@ -69,7 +69,7 @@ public void correctSidsAreRetrieved() {
6969
public void roleHierarchyIsUsedWhenSet() {
7070
RoleHierarchy rh = mock(RoleHierarchy.class);
7171
List rhAuthorities = AuthorityUtils.createAuthorityList("D");
72-
when(rh.getReachableGrantedAuthorities(anyCollection())).thenReturn(rhAuthorities);
72+
given(rh.getReachableGrantedAuthorities(anyCollection())).willReturn(rhAuthorities);
7373
SidRetrievalStrategy strat = new SidRetrievalStrategyImpl(rh);
7474

7575
List<Sid> sids = strat.getSids(this.authentication);

0 commit comments

Comments
 (0)