1
0
mirror of synced 2026-08-05 17:57:15 +00:00

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
@@ -40,9 +40,9 @@ import org.springframework.web.context.WebApplicationContext;
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;
/**
* @author Rob Winch
@@ -89,8 +89,8 @@ public class AbstractAuthorizeTagTests {
WebInvocationPrivilegeEvaluator expected = mock(WebInvocationPrivilegeEvaluator.class);
this.tag.setUrl(uri);
WebApplicationContext wac = mock(WebApplicationContext.class);
when(wac.getBeansOfType(WebInvocationPrivilegeEvaluator.class))
.thenReturn(Collections.singletonMap("wipe", expected));
given(wac.getBeansOfType(WebInvocationPrivilegeEvaluator.class))
.willReturn(Collections.singletonMap("wipe", expected));
this.servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
this.tag.authorizeUsingUrlCheck();
@@ -105,8 +105,8 @@ public class AbstractAuthorizeTagTests {
DefaultWebSecurityExpressionHandler expected = new DefaultWebSecurityExpressionHandler();
this.tag.setAccess("permitAll");
WebApplicationContext wac = mock(WebApplicationContext.class);
when(wac.getBeansOfType(SecurityExpressionHandler.class))
.thenReturn(Collections.<String, SecurityExpressionHandler>singletonMap("wipe", expected));
given(wac.getBeansOfType(SecurityExpressionHandler.class))
.willReturn(Collections.<String, SecurityExpressionHandler>singletonMap("wipe", expected));
this.servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
assertThat(this.tag.authorize()).isTrue();
@@ -36,10 +36,10 @@ import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.context.WebApplicationContext;
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.verifyNoMoreInteractions;
import static org.mockito.Mockito.when;
/**
* @author Luke Taylor
@@ -68,7 +68,7 @@ public class AccessControlListTagTests {
Map beanMap = new HashMap();
beanMap.put("pe", this.pe);
when(ctx.getBeansOfType(PermissionEvaluator.class)).thenReturn(beanMap);
given(ctx.getBeansOfType(PermissionEvaluator.class)).willReturn(beanMap);
MockServletContext servletCtx = new MockServletContext();
servletCtx.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx);
@@ -84,7 +84,7 @@ public class AccessControlListTagTests {
@Test
public void bodyIsEvaluatedIfAclGrantsAccess() throws Exception {
Object domainObject = new Object();
when(this.pe.hasPermission(this.bob, domainObject, "READ")).thenReturn(true);
given(this.pe.hasPermission(this.bob, domainObject, "READ")).willReturn(true);
this.tag.setDomainObject(domainObject);
this.tag.setHasPermission("READ");
@@ -105,7 +105,7 @@ public class AccessControlListTagTests {
servletContext.setAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.dispatcher", wac);
Object domainObject = new Object();
when(this.pe.hasPermission(this.bob, domainObject, "READ")).thenReturn(true);
given(this.pe.hasPermission(this.bob, domainObject, "READ")).willReturn(true);
this.tag.setDomainObject(domainObject);
this.tag.setHasPermission("READ");
@@ -121,8 +121,8 @@ public class AccessControlListTagTests {
@Test
public void multiHasPermissionsAreSplit() throws Exception {
Object domainObject = new Object();
when(this.pe.hasPermission(this.bob, domainObject, "READ")).thenReturn(true);
when(this.pe.hasPermission(this.bob, domainObject, "WRITE")).thenReturn(true);
given(this.pe.hasPermission(this.bob, domainObject, "READ")).willReturn(true);
given(this.pe.hasPermission(this.bob, domainObject, "WRITE")).willReturn(true);
this.tag.setDomainObject(domainObject);
this.tag.setHasPermission("READ,WRITE");
@@ -141,8 +141,8 @@ public class AccessControlListTagTests {
@Test
public void hasPermissionsBitMaskSupported() throws Exception {
Object domainObject = new Object();
when(this.pe.hasPermission(this.bob, domainObject, 1)).thenReturn(true);
when(this.pe.hasPermission(this.bob, domainObject, 2)).thenReturn(true);
given(this.pe.hasPermission(this.bob, domainObject, 1)).willReturn(true);
given(this.pe.hasPermission(this.bob, domainObject, 2)).willReturn(true);
this.tag.setDomainObject(domainObject);
this.tag.setHasPermission("1,2");
@@ -160,8 +160,8 @@ public class AccessControlListTagTests {
@Test
public void hasPermissionsMixedBitMaskSupported() throws Exception {
Object domainObject = new Object();
when(this.pe.hasPermission(this.bob, domainObject, 1)).thenReturn(true);
when(this.pe.hasPermission(this.bob, domainObject, "WRITE")).thenReturn(true);
given(this.pe.hasPermission(this.bob, domainObject, 1)).willReturn(true);
given(this.pe.hasPermission(this.bob, domainObject, "WRITE")).willReturn(true);
this.tag.setDomainObject(domainObject);
this.tag.setHasPermission("1,WRITE");
@@ -179,7 +179,7 @@ public class AccessControlListTagTests {
@Test
public void bodyIsSkippedIfAclDeniesAccess() throws Exception {
Object domainObject = new Object();
when(this.pe.hasPermission(this.bob, domainObject, "READ")).thenReturn(false);
given(this.pe.hasPermission(this.bob, domainObject, "READ")).willReturn(false);
this.tag.setDomainObject(domainObject);
this.tag.setHasPermission("READ");
@@ -43,7 +43,7 @@ import org.springframework.web.context.support.StaticWebApplicationContext;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.when;
import static org.mockito.BDDMockito.given;
/**
* @author Francois Beausoleil
@@ -91,7 +91,7 @@ public class AuthorizeTagTests {
Object domain = new Object();
this.request.setAttribute("domain", domain);
this.authorizeTag.setAccess("hasPermission(#domain,'read') or hasPermission(#domain,'write')");
when(this.permissionEvaluator.hasPermission(eq(this.currentUser), eq(domain), anyString())).thenReturn(true);
given(this.permissionEvaluator.hasPermission(eq(this.currentUser), eq(domain), anyString())).willReturn(true);
assertThat(this.authorizeTag.doStartTag()).isEqualTo(Tag.EVAL_BODY_INCLUDE);
}