Clean up Mockito usage

This commit migrates to the MockitoJUnitRunner where sensible, which
will later allow for an easier migration to Mockito's extension for
JUnit Jupiter.

In addition, this commit deletes unnecessary stubbing for various mocks
and polishes test fixture setup in various test classes.
This commit is contained in:
Sam Brannen
2019-05-28 13:56:10 +02:00
parent d495902a9c
commit 141ef9082f
27 changed files with 170 additions and 290 deletions

View File

@@ -19,9 +19,7 @@ package org.springframework.web.socket.config.annotation;
import java.util.ArrayList;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.scheduling.TaskScheduler;
import org.springframework.web.socket.WebSocketHandler;
@@ -37,26 +35,20 @@ import org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsServ
import org.springframework.web.socket.sockjs.transport.handler.WebSocketTransportHandler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Test fixture for
* {@link org.springframework.web.socket.config.annotation.AbstractWebSocketHandlerRegistration}.
* Test fixture for {@link AbstractWebSocketHandlerRegistration}.
*
* @author Rossen Stoyanchev
*/
public class WebSocketHandlerRegistrationTests {
private TestWebSocketHandlerRegistration registration;
private TestWebSocketHandlerRegistration registration = new TestWebSocketHandlerRegistration();
private TaskScheduler taskScheduler;
private TaskScheduler taskScheduler = mock(TaskScheduler.class);
@Before
public void setup() {
this.taskScheduler = Mockito.mock(TaskScheduler.class);
this.registration = new TestWebSocketHandlerRegistration();
}
@Test
public void minimal() {
WebSocketHandler handler = new TextWebSocketHandler();

View File

@@ -21,8 +21,9 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.beans.DirectFieldAccessor;
import org.springframework.messaging.MessageChannel;
@@ -46,12 +47,9 @@ import static org.mockito.Mockito.verify;
* @author Rossen Stoyanchev
* @author Andy Wilkinson
*/
@RunWith(MockitoJUnitRunner.class)
public class SubProtocolWebSocketHandlerTests {
private SubProtocolWebSocketHandler webSocketHandler;
private TestWebSocketSession session;
@Mock SubProtocolHandler stompHandler;
@Mock SubProtocolHandler mqttHandler;
@@ -63,10 +61,13 @@ public class SubProtocolWebSocketHandlerTests {
@Mock
SubscribableChannel outClientChannel;
private SubProtocolWebSocketHandler webSocketHandler;
private TestWebSocketSession session;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
this.webSocketHandler = new SubProtocolWebSocketHandler(this.inClientChannel, this.outClientChannel);
given(stompHandler.getSupportedProtocols()).willReturn(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"));
given(mqttHandler.getSupportedProtocols()).willReturn(Arrays.asList("MQTT"));

View File

@@ -22,9 +22,10 @@ import java.util.concurrent.ScheduledFuture;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.messaging.Message;
import org.springframework.messaging.simp.stomp.ConnectionHandlingStompSession;
@@ -63,6 +64,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
*
* @author Rossen Stoyanchev
*/
@RunWith(MockitoJUnitRunner.class)
public class WebSocketStompClientTests {
@Mock
@@ -74,7 +76,6 @@ public class WebSocketStompClientTests {
@Mock
private WebSocketSession webSocketSession;
private TestWebSocketStompClient stompClient;
private ArgumentCaptor<WebSocketHandler> webSocketHandlerCaptor;
@@ -84,8 +85,6 @@ public class WebSocketStompClientTests {
@Before
public void setUp() throws Exception {
MockitoAnnotations.initMocks(this);
WebSocketClient webSocketClient = mock(WebSocketClient.class);
this.stompClient = new TestWebSocketStompClient(webSocketClient);
this.stompClient.setTaskScheduler(this.taskScheduler);

View File

@@ -21,10 +21,7 @@ import java.util.Collections;
import java.util.List;
import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.web.socket.AbstractHttpRequestTests;
import org.springframework.web.socket.SubProtocolCapable;
@@ -35,6 +32,7 @@ import org.springframework.web.socket.handler.TextWebSocketHandler;
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
/**
@@ -44,19 +42,9 @@ import static org.mockito.Mockito.verify;
*/
public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests {
private DefaultHandshakeHandler handshakeHandler;
private RequestUpgradeStrategy upgradeStrategy = mock(RequestUpgradeStrategy.class);
@Mock
private RequestUpgradeStrategy upgradeStrategy;
@Before
public void setup() {
super.setup();
MockitoAnnotations.initMocks(this);
this.handshakeHandler = new DefaultHandshakeHandler(this.upgradeStrategy);
}
private DefaultHandshakeHandler handshakeHandler = new DefaultHandshakeHandler(this.upgradeStrategy);
@Test

View File

@@ -22,13 +22,13 @@ import java.util.Map;
import java.util.Set;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.mock.web.test.MockHttpSession;
import org.springframework.web.socket.AbstractHttpRequestTests;
import org.springframework.web.socket.WebSocketHandler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.mock;
/**
* Test fixture for {@link HttpSessionHandshakeInterceptor}.
@@ -37,12 +37,12 @@ import static org.assertj.core.api.Assertions.assertThat;
*/
public class HttpSessionHandshakeInterceptorTests extends AbstractHttpRequestTests {
private final Map<String, Object> attributes = new HashMap<>();
private final WebSocketHandler wsHandler = mock(WebSocketHandler.class);
@Test
public void defaultConstructor() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
this.servletRequest.getSession().setAttribute("bar", "baz");
@@ -58,9 +58,6 @@ public class HttpSessionHandshakeInterceptorTests extends AbstractHttpRequestTes
@Test
public void constructorWithAttributeNames() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
this.servletRequest.getSession().setAttribute("bar", "baz");
@@ -76,9 +73,6 @@ public class HttpSessionHandshakeInterceptorTests extends AbstractHttpRequestTes
@Test
public void doNotCopyHttpSessionId() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
@@ -93,9 +87,6 @@ public class HttpSessionHandshakeInterceptorTests extends AbstractHttpRequestTes
@Test
public void doNotCopyAttributes() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.setSession(new MockHttpSession(null, "123"));
this.servletRequest.getSession().setAttribute("foo", "bar");
@@ -109,9 +100,6 @@ public class HttpSessionHandshakeInterceptorTests extends AbstractHttpRequestTes
@Test
public void doNotCauseSessionCreation() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
HttpSessionHandshakeInterceptor interceptor = new HttpSessionHandshakeInterceptor();
interceptor.beforeHandshake(this.request, this.response, wsHandler, attributes);

View File

@@ -25,7 +25,6 @@ import java.util.Set;
import java.util.concurrent.ConcurrentSkipListSet;
import org.junit.Test;
import org.mockito.Mockito;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
@@ -34,6 +33,7 @@ import org.springframework.web.socket.WebSocketHandler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
import static org.mockito.Mockito.mock;
/**
* Test fixture for {@link OriginHandshakeInterceptor}.
@@ -42,16 +42,17 @@ import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException
*/
public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
private final Map<String, Object> attributes = new HashMap<>();
private final WebSocketHandler wsHandler = mock(WebSocketHandler.class);
@Test
public void invalidInput() {
assertThatIllegalArgumentException().isThrownBy(() ->
new OriginHandshakeInterceptor(null));
assertThatIllegalArgumentException().isThrownBy(() -> new OriginHandshakeInterceptor(null));
}
@Test
public void originValueMatch() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "https://mydomain1.com");
List<String> allowed = Collections.singletonList("https://mydomain1.com");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
@@ -61,8 +62,6 @@ public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
@Test
public void originValueNoMatch() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "https://mydomain1.com");
List<String> allowed = Collections.singletonList("https://mydomain2.com");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
@@ -72,8 +71,6 @@ public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
@Test
public void originListMatch() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "https://mydomain2.com");
List<String> allowed = Arrays.asList("https://mydomain1.com", "https://mydomain2.com", "http://mydomain3.com");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
@@ -83,8 +80,6 @@ public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
@Test
public void originListNoMatch() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://www.mydomain4.com/");
List<String> allowed = Arrays.asList("https://mydomain1.com", "https://mydomain2.com", "http://mydomain3.com");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(allowed);
@@ -94,8 +89,6 @@ public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
@Test
public void originNoMatchWithNullHostileCollection() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://www.mydomain4.com/");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor();
Set<String> allowedOrigins = new ConcurrentSkipListSet<>();
@@ -107,8 +100,6 @@ public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
@Test
public void originMatchAll() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "https://mydomain1.com");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor();
interceptor.setAllowedOrigins(Collections.singletonList("*"));
@@ -118,8 +109,6 @@ public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
@Test
public void sameOriginMatchWithEmptyAllowedOrigins() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
this.servletRequest.setServerName("mydomain2.com");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Collections.emptyList());
@@ -129,8 +118,6 @@ public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
@Test
public void sameOriginMatchWithAllowedOrigins() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain2.com");
this.servletRequest.setServerName("mydomain2.com");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Arrays.asList("http://mydomain1.com"));
@@ -140,8 +127,6 @@ public class OriginHandshakeInterceptorTests extends AbstractHttpRequestTests {
@Test
public void sameOriginNoMatch() throws Exception {
Map<String, Object> attributes = new HashMap<>();
WebSocketHandler wsHandler = Mockito.mock(WebSocketHandler.class);
this.servletRequest.addHeader(HttpHeaders.ORIGIN, "http://mydomain3.com");
this.servletRequest.setServerName("mydomain2.com");
OriginHandshakeInterceptor interceptor = new OriginHandshakeInterceptor(Collections.emptyList());

View File

@@ -23,8 +23,9 @@ import java.util.Map;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.junit.MockitoJUnitRunner;
import org.springframework.http.HttpHeaders;
import org.springframework.scheduling.TaskScheduler;
@@ -57,6 +58,7 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
* @author Sebastien Deleuze
* @author Ben Kiefer
*/
@RunWith(MockitoJUnitRunner.class)
public class DefaultSockJsServiceTests extends AbstractHttpRequestTests {
private static final String sockJsPrefix = "/mysockjs";
@@ -66,25 +68,30 @@ public class DefaultSockJsServiceTests extends AbstractHttpRequestTests {
private static final String sessionUrlPrefix = "/server1/" + sessionId + "/";
@Mock private SessionCreatingTransportHandler xhrHandler;
@Mock
private SessionCreatingTransportHandler xhrHandler;
@Mock private TransportHandler xhrSendHandler;
@Mock
private TransportHandler xhrSendHandler;
@Mock private HandshakeTransportHandler wsTransportHandler;
@Mock
private HandshakeTransportHandler wsTransportHandler;
@Mock private WebSocketHandler wsHandler;
@Mock
private WebSocketHandler wsHandler;
@Mock private TaskScheduler taskScheduler;
@Mock
private TaskScheduler taskScheduler;
private TestSockJsSession session;
private TransportHandlingSockJsService service;
@Override
@Before
public void setup() {
super.setup();
MockitoAnnotations.initMocks(this);
Map<String, Object> attributes = Collections.emptyMap();
this.session = new TestSockJsSession(sessionId, new StubSockJsServiceConfig(), this.wsHandler, attributes);