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:
@@ -35,7 +35,7 @@ import org.springframework.security.core.Authentication;
|
||||
import org.springframework.security.core.authority.AuthorityUtils;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class DefaultMessageSecurityExpressionHandlerTests {
|
||||
@@ -80,7 +80,7 @@ public class DefaultMessageSecurityExpressionHandlerTests {
|
||||
this.handler.setTrustResolver(this.trustResolver);
|
||||
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
|
||||
Expression expression = this.handler.getExpressionParser().parseExpression("authenticated");
|
||||
when(this.trustResolver.isAnonymous(this.authentication)).thenReturn(false);
|
||||
given(this.trustResolver.isAnonymous(this.authentication)).willReturn(false);
|
||||
|
||||
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
|
||||
}
|
||||
@@ -102,7 +102,7 @@ public class DefaultMessageSecurityExpressionHandlerTests {
|
||||
this.handler.setPermissionEvaluator(this.permissionEvaluator);
|
||||
EvaluationContext context = this.handler.createEvaluationContext(this.authentication, this.message);
|
||||
Expression expression = this.handler.getExpressionParser().parseExpression("hasPermission(message, 'read')");
|
||||
when(this.permissionEvaluator.hasPermission(this.authentication, this.message, "read")).thenReturn(true);
|
||||
given(this.permissionEvaluator.hasPermission(this.authentication, this.message, "read")).willReturn(true);
|
||||
|
||||
assertThat(ExpressionUtils.evaluateAsBoolean(expression, context)).isTrue();
|
||||
}
|
||||
|
||||
@@ -30,9 +30,9 @@ import org.springframework.security.messaging.util.matcher.MessageMatcher;
|
||||
import org.springframework.security.messaging.util.matcher.SimpDestinationMessageMatcher;
|
||||
|
||||
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.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class MessageExpressionConfigAttributeTests {
|
||||
@@ -72,7 +72,7 @@ public class MessageExpressionConfigAttributeTests {
|
||||
|
||||
@Test
|
||||
public void toStringUsesExpressionString() {
|
||||
when(this.expression.getExpressionString()).thenReturn("toString");
|
||||
given(this.expression.getExpressionString()).willReturn("toString");
|
||||
|
||||
assertThat(this.attribute.toString()).isEqualTo(this.expression.getExpressionString());
|
||||
}
|
||||
|
||||
@@ -36,9 +36,9 @@ import org.springframework.security.messaging.util.matcher.MessageMatcher;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.springframework.security.access.AccessDecisionVoter.ACCESS_ABSTAIN;
|
||||
import static org.springframework.security.access.AccessDecisionVoter.ACCESS_DENIED;
|
||||
import static org.springframework.security.access.AccessDecisionVoter.ACCESS_GRANTED;
|
||||
@@ -78,13 +78,13 @@ public class MessageExpressionVoterTests {
|
||||
|
||||
@Test
|
||||
public void voteGranted() {
|
||||
when(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(true);
|
||||
given(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).willReturn(true);
|
||||
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void voteDenied() {
|
||||
when(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(false);
|
||||
given(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).willReturn(false);
|
||||
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_DENIED);
|
||||
}
|
||||
|
||||
@@ -122,9 +122,9 @@ public class MessageExpressionVoterTests {
|
||||
@Test
|
||||
public void customExpressionHandler() {
|
||||
this.voter.setExpressionHandler(this.expressionHandler);
|
||||
when(this.expressionHandler.createEvaluationContext(this.authentication, this.message))
|
||||
.thenReturn(this.evaluationContext);
|
||||
when(this.expression.getValue(this.evaluationContext, Boolean.class)).thenReturn(true);
|
||||
given(this.expressionHandler.createEvaluationContext(this.authentication, this.message))
|
||||
.willReturn(this.evaluationContext);
|
||||
given(this.expression.getValue(this.evaluationContext, Boolean.class)).willReturn(true);
|
||||
|
||||
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
|
||||
|
||||
@@ -135,12 +135,12 @@ public class MessageExpressionVoterTests {
|
||||
public void postProcessEvaluationContext() {
|
||||
final MessageExpressionConfigAttribute configAttribute = mock(MessageExpressionConfigAttribute.class);
|
||||
this.voter.setExpressionHandler(this.expressionHandler);
|
||||
when(this.expressionHandler.createEvaluationContext(this.authentication, this.message))
|
||||
.thenReturn(this.evaluationContext);
|
||||
when(configAttribute.getAuthorizeExpression()).thenReturn(this.expression);
|
||||
given(this.expressionHandler.createEvaluationContext(this.authentication, this.message))
|
||||
.willReturn(this.evaluationContext);
|
||||
given(configAttribute.getAuthorizeExpression()).willReturn(this.expression);
|
||||
this.attributes = Arrays.<ConfigAttribute>asList(configAttribute);
|
||||
when(configAttribute.postProcess(this.evaluationContext, this.message)).thenReturn(this.evaluationContext);
|
||||
when(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).thenReturn(true);
|
||||
given(configAttribute.postProcess(this.evaluationContext, this.message)).willReturn(this.evaluationContext);
|
||||
given(this.expression.getValue(any(EvaluationContext.class), eq(Boolean.class))).willReturn(true);
|
||||
|
||||
assertThat(this.voter.vote(this.authentication, this.message, this.attributes)).isEqualTo(ACCESS_GRANTED);
|
||||
verify(configAttribute).postProcess(this.evaluationContext, this.message);
|
||||
|
||||
@@ -40,8 +40,8 @@ import org.springframework.security.core.context.SecurityContextHolder;
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doThrow;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.BDDMockito.willThrow;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class ChannelSecurityInterceptorTests {
|
||||
@@ -108,7 +108,7 @@ public class ChannelSecurityInterceptorTests {
|
||||
|
||||
@Test
|
||||
public void preSendGrant() {
|
||||
when(this.source.getAttributes(this.message)).thenReturn(this.attrs);
|
||||
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
|
||||
|
||||
Message<?> result = this.interceptor.preSend(this.message, this.channel);
|
||||
|
||||
@@ -117,8 +117,8 @@ public class ChannelSecurityInterceptorTests {
|
||||
|
||||
@Test(expected = AccessDeniedException.class)
|
||||
public void preSendDeny() {
|
||||
when(this.source.getAttributes(this.message)).thenReturn(this.attrs);
|
||||
doThrow(new AccessDeniedException("")).when(this.accessDecisionManager).decide(any(Authentication.class),
|
||||
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
|
||||
willThrow(new AccessDeniedException("")).given(this.accessDecisionManager).decide(any(Authentication.class),
|
||||
eq(this.message), eq(this.attrs));
|
||||
|
||||
this.interceptor.preSend(this.message, this.channel);
|
||||
@@ -127,9 +127,9 @@ public class ChannelSecurityInterceptorTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void preSendPostSendRunAs() {
|
||||
when(this.source.getAttributes(this.message)).thenReturn(this.attrs);
|
||||
when(this.runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class)))
|
||||
.thenReturn(this.runAs);
|
||||
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
|
||||
given(this.runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class)))
|
||||
.willReturn(this.runAs);
|
||||
|
||||
Message<?> preSend = this.interceptor.preSend(this.message, this.channel);
|
||||
|
||||
@@ -148,9 +148,9 @@ public class ChannelSecurityInterceptorTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
@Test
|
||||
public void preSendFinallySendRunAs() {
|
||||
when(this.source.getAttributes(this.message)).thenReturn(this.attrs);
|
||||
when(this.runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class)))
|
||||
.thenReturn(this.runAs);
|
||||
given(this.source.getAttributes(this.message)).willReturn(this.attrs);
|
||||
given(this.runAsManager.buildRunAs(any(Authentication.class), any(), any(Collection.class)))
|
||||
.willReturn(this.runAs);
|
||||
|
||||
Message<?> preSend = this.interceptor.preSend(this.message, this.channel);
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class AndMessageMatcherTests {
|
||||
@@ -76,7 +76,7 @@ public class AndMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesSingleTrue() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(true);
|
||||
given(this.delegate.matches(this.message)).willReturn(true);
|
||||
this.matcher = new AndMessageMatcher<>(this.delegate);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isTrue();
|
||||
@@ -84,8 +84,8 @@ public class AndMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesMultiTrue() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(true);
|
||||
when(this.delegate2.matches(this.message)).thenReturn(true);
|
||||
given(this.delegate.matches(this.message)).willReturn(true);
|
||||
given(this.delegate2.matches(this.message)).willReturn(true);
|
||||
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isTrue();
|
||||
@@ -93,7 +93,7 @@ public class AndMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesSingleFalse() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(false);
|
||||
given(this.delegate.matches(this.message)).willReturn(false);
|
||||
this.matcher = new AndMessageMatcher<>(this.delegate);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isFalse();
|
||||
@@ -101,7 +101,7 @@ public class AndMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesMultiBothFalse() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(false);
|
||||
given(this.delegate.matches(this.message)).willReturn(false);
|
||||
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isFalse();
|
||||
@@ -109,8 +109,8 @@ public class AndMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesMultiSingleFalse() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(true);
|
||||
when(this.delegate2.matches(this.message)).thenReturn(false);
|
||||
given(this.delegate.matches(this.message)).willReturn(true);
|
||||
given(this.delegate2.matches(this.message)).willReturn(false);
|
||||
this.matcher = new AndMessageMatcher<>(this.delegate, this.delegate2);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isFalse();
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.mockito.junit.MockitoJUnitRunner;
|
||||
import org.springframework.messaging.Message;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThat;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.mockito.BDDMockito.given;
|
||||
|
||||
@RunWith(MockitoJUnitRunner.class)
|
||||
public class OrMessageMatcherTests {
|
||||
@@ -76,7 +76,7 @@ public class OrMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesSingleTrue() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(true);
|
||||
given(this.delegate.matches(this.message)).willReturn(true);
|
||||
this.matcher = new OrMessageMatcher<>(this.delegate);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isTrue();
|
||||
@@ -84,7 +84,7 @@ public class OrMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesMultiTrue() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(true);
|
||||
given(this.delegate.matches(this.message)).willReturn(true);
|
||||
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isTrue();
|
||||
@@ -92,7 +92,7 @@ public class OrMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesSingleFalse() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(false);
|
||||
given(this.delegate.matches(this.message)).willReturn(false);
|
||||
this.matcher = new OrMessageMatcher<>(this.delegate);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isFalse();
|
||||
@@ -100,8 +100,8 @@ public class OrMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesMultiBothFalse() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(false);
|
||||
when(this.delegate2.matches(this.message)).thenReturn(false);
|
||||
given(this.delegate.matches(this.message)).willReturn(false);
|
||||
given(this.delegate2.matches(this.message)).willReturn(false);
|
||||
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isFalse();
|
||||
@@ -109,7 +109,7 @@ public class OrMessageMatcherTests {
|
||||
|
||||
@Test
|
||||
public void matchesMultiSingleFalse() {
|
||||
when(this.delegate.matches(this.message)).thenReturn(true);
|
||||
given(this.delegate.matches(this.message)).willReturn(true);
|
||||
this.matcher = new OrMessageMatcher<>(this.delegate, this.delegate2);
|
||||
|
||||
assertThat(this.matcher.matches(this.message)).isTrue();
|
||||
|
||||
Reference in New Issue
Block a user