Skip to content

Commit ee231a5

Browse files
committed
Merged changes from Example 2: Avoid duplicate creation of mocks in org.springframework.security.authorization.method
1 parent a831ec9 commit ee231a5

5 files changed

+26
-16
lines changed

core/src/test/java/org/springframework/security/authorization/method/AuthorizationManagerAfterMethodInterceptorTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -82,9 +82,8 @@ public void beforeWhenMockAuthorizationManagerThenCheckAndReturnedObject() throw
8282

8383
@Test
8484
public void afterWhenMockSecurityContextHolderStrategyThenUses() throws Throwable {
85-
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
8685
Authentication authentication = TestAuthentication.authenticatedUser();
87-
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
86+
SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));
8887
MethodInvocation invocation = mock(MethodInvocation.class);
8988
AuthorizationManager<MethodInvocationResult> authorizationManager = AuthenticatedAuthorizationManager
9089
.authenticated();
@@ -98,10 +97,9 @@ public void afterWhenMockSecurityContextHolderStrategyThenUses() throws Throwabl
9897
// gh-12877
9998
@Test
10099
public void afterWhenStaticSecurityContextHolderStrategyAfterConstructorThenUses() throws Throwable {
101-
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
102100
Authentication authentication = new TestingAuthenticationToken("john", "password",
103101
AuthorityUtils.createAuthorityList("authority"));
104-
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
102+
SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));
105103
MethodInvocation invocation = mock(MethodInvocation.class);
106104
AuthorizationManager<MethodInvocationResult> authorizationManager = AuthenticatedAuthorizationManager
107105
.authenticated();

core/src/test/java/org/springframework/security/authorization/method/AuthorizationManagerBeforeMethodInterceptorTests.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -77,10 +77,9 @@ public void beforeWhenMockAuthorizationManagerThenCheck() throws Throwable {
7777

7878
@Test
7979
public void beforeWhenMockSecurityContextHolderStrategyThenUses() throws Throwable {
80-
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
8180
Authentication authentication = new TestingAuthenticationToken("user", "password",
8281
AuthorityUtils.createAuthorityList("authority"));
83-
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
82+
SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));
8483
MethodInvocation invocation = mock(MethodInvocation.class);
8584
AuthorizationManager<MethodInvocation> authorizationManager = AuthenticatedAuthorizationManager.authenticated();
8685
AuthorizationManagerBeforeMethodInterceptor advice = new AuthorizationManagerBeforeMethodInterceptor(
@@ -93,10 +92,10 @@ public void beforeWhenMockSecurityContextHolderStrategyThenUses() throws Throwab
9392
// gh-12877
9493
@Test
9594
public void beforeWhenStaticSecurityContextHolderStrategyAfterConstructorThenUses() throws Throwable {
96-
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
95+
9796
Authentication authentication = new TestingAuthenticationToken("john", "password",
9897
AuthorityUtils.createAuthorityList("authority"));
99-
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
98+
SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));
10099
MethodInvocation invocation = mock(MethodInvocation.class);
101100
AuthorizationManager<MethodInvocation> authorizationManager = AuthenticatedAuthorizationManager.authenticated();
102101
AuthorizationManagerBeforeMethodInterceptor advice = new AuthorizationManagerBeforeMethodInterceptor(
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package org.springframework.security.authorization.method;
2+
3+
import org.springframework.security.core.context.*;
4+
5+
import static org.mockito.BDDMockito.*;
6+
import static org.mockito.Mockito.*;
7+
8+
public class MockSecurityContextHolderStrategy {
9+
static SecurityContextHolderStrategy getmock(SecurityContextImpl securityContextImpl){
10+
11+
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
12+
given(strategy.getContext()).willReturn(securityContextImpl);
13+
return strategy;
14+
}
15+
}

core/src/test/java/org/springframework/security/authorization/method/PostFilterAuthorizationMethodInterceptorTests.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -129,10 +129,10 @@ public void checkInheritedAnnotationsWhenConflictingThenAnnotationConfigurationE
129129

130130
@Test
131131
public void postFilterWhenMockSecurityContextHolderStrategyThenUses() throws Throwable {
132-
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
132+
133133
Authentication authentication = new TestingAuthenticationToken("john", "password",
134134
AuthorityUtils.createAuthorityList("authority"));
135-
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
135+
SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));
136136
String[] array = { "john", "bob" };
137137
MockMethodInvocation invocation = new MockMethodInvocation(new TestClass(), TestClass.class,
138138
"doSomethingArrayAuthentication", new Class[] { String[].class }, new Object[] { array }) {
@@ -150,10 +150,10 @@ public Object proceed() {
150150
// gh-12877
151151
@Test
152152
public void postFilterWhenStaticSecurityContextHolderStrategyAfterConstructorThenUses() throws Throwable {
153-
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
153+
154154
Authentication authentication = new TestingAuthenticationToken("john", "password",
155155
AuthorityUtils.createAuthorityList("authority"));
156-
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
156+
SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));
157157
String[] array = { "john", "bob" };
158158
MockMethodInvocation invocation = new MockMethodInvocation(new TestClass(), TestClass.class,
159159
"doSomethingArrayAuthentication", new Class[] { String[].class }, new Object[] { array }) {

core/src/test/java/org/springframework/security/authorization/method/PreFilterAuthorizationMethodInterceptorTests.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -189,10 +189,9 @@ public void checkInheritedAnnotationsWhenConflictingThenAnnotationConfigurationE
189189

190190
@Test
191191
public void preFilterWhenMockSecurityContextHolderStrategyThenUses() throws Throwable {
192-
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
193192
Authentication authentication = new TestingAuthenticationToken("john", "password",
194193
AuthorityUtils.createAuthorityList("authority"));
195-
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
194+
SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));
196195
List<String> list = new ArrayList<>();
197196
list.add("john");
198197
list.add("bob");
@@ -207,10 +206,9 @@ public void preFilterWhenMockSecurityContextHolderStrategyThenUses() throws Thro
207206
// gh-12877
208207
@Test
209208
public void preFilterWhenStaticSecurityContextHolderStrategyAfterConstructorThenUses() throws Throwable {
210-
SecurityContextHolderStrategy strategy = mock(SecurityContextHolderStrategy.class);
211209
Authentication authentication = new TestingAuthenticationToken("john", "password",
212210
AuthorityUtils.createAuthorityList("authority"));
213-
given(strategy.getContext()).willReturn(new SecurityContextImpl(authentication));
211+
SecurityContextHolderStrategy strategy = MockSecurityContextHolderStrategy.getmock(new SecurityContextImpl(authentication));
214212
List<String> list = new ArrayList<>();
215213
list.add("john");
216214
list.add("bob");

0 commit comments

Comments
 (0)