Polish mockito usage
Consistent use of BDDMockito rather than standard Mockito.
This commit is contained in:
@@ -24,10 +24,8 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter;
|
||||
import org.springframework.web.socket.adapter.jetty.JettyWebSocketSession;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link org.springframework.web.socket.adapter.jetty.JettyWebSocketHandlerAdapter}.
|
||||
@@ -48,8 +46,8 @@ public class JettyWebSocketHandlerAdapterTests {
|
||||
@Before
|
||||
public void setup() {
|
||||
this.session = mock(Session.class);
|
||||
when(this.session.getUpgradeRequest()).thenReturn(Mockito.mock(UpgradeRequest.class));
|
||||
when(this.session.getUpgradeResponse()).thenReturn(Mockito.mock(UpgradeResponse.class));
|
||||
given(this.session.getUpgradeRequest()).willReturn(Mockito.mock(UpgradeRequest.class));
|
||||
given(this.session.getUpgradeResponse()).willReturn(Mockito.mock(UpgradeResponse.class));
|
||||
|
||||
this.webSocketHandler = mock(WebSocketHandler.class);
|
||||
this.webSocketSession = new JettyWebSocketSession(null, null);
|
||||
|
||||
@@ -16,6 +16,9 @@
|
||||
|
||||
package org.springframework.web.socket.adapter.jetty;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.eclipse.jetty.websocket.api.Session;
|
||||
import org.eclipse.jetty.websocket.api.UpgradeRequest;
|
||||
import org.eclipse.jetty.websocket.api.UpgradeResponse;
|
||||
@@ -24,14 +27,8 @@ import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.web.socket.handler.TestPrincipal;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.web.socket.adapter.jetty.JettyWebSocketSession}.
|
||||
@@ -63,14 +60,14 @@ public class JettyWebSocketSessionTests {
|
||||
TestPrincipal user = new TestPrincipal("joe");
|
||||
|
||||
UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
|
||||
when(request.getUserPrincipal()).thenReturn(user);
|
||||
given(request.getUserPrincipal()).willReturn(user);
|
||||
|
||||
UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
|
||||
when(response.getAcceptedSubProtocol()).thenReturn(null);
|
||||
given(response.getAcceptedSubProtocol()).willReturn(null);
|
||||
|
||||
Session nativeSession = Mockito.mock(Session.class);
|
||||
when(nativeSession.getUpgradeRequest()).thenReturn(request);
|
||||
when(nativeSession.getUpgradeResponse()).thenReturn(response);
|
||||
given(nativeSession.getUpgradeRequest()).willReturn(request);
|
||||
given(nativeSession.getUpgradeResponse()).willReturn(response);
|
||||
|
||||
JettyWebSocketSession session = new JettyWebSocketSession(attributes);
|
||||
session.initializeNativeSession(nativeSession);
|
||||
@@ -85,14 +82,14 @@ public class JettyWebSocketSessionTests {
|
||||
public void getPrincipalNotAvailable() {
|
||||
|
||||
UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
|
||||
when(request.getUserPrincipal()).thenReturn(null);
|
||||
given(request.getUserPrincipal()).willReturn(null);
|
||||
|
||||
UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
|
||||
when(response.getAcceptedSubProtocol()).thenReturn(null);
|
||||
given(response.getAcceptedSubProtocol()).willReturn(null);
|
||||
|
||||
Session nativeSession = Mockito.mock(Session.class);
|
||||
when(nativeSession.getUpgradeRequest()).thenReturn(request);
|
||||
when(nativeSession.getUpgradeResponse()).thenReturn(response);
|
||||
given(nativeSession.getUpgradeRequest()).willReturn(request);
|
||||
given(nativeSession.getUpgradeResponse()).willReturn(response);
|
||||
|
||||
JettyWebSocketSession session = new JettyWebSocketSession(attributes);
|
||||
session.initializeNativeSession(nativeSession);
|
||||
@@ -109,14 +106,14 @@ public class JettyWebSocketSessionTests {
|
||||
String protocol = "foo";
|
||||
|
||||
UpgradeRequest request = Mockito.mock(UpgradeRequest.class);
|
||||
when(request.getUserPrincipal()).thenReturn(null);
|
||||
given(request.getUserPrincipal()).willReturn(null);
|
||||
|
||||
UpgradeResponse response = Mockito.mock(UpgradeResponse.class);
|
||||
when(response.getAcceptedSubProtocol()).thenReturn(protocol);
|
||||
given(response.getAcceptedSubProtocol()).willReturn(protocol);
|
||||
|
||||
Session nativeSession = Mockito.mock(Session.class);
|
||||
when(nativeSession.getUpgradeRequest()).thenReturn(request);
|
||||
when(nativeSession.getUpgradeResponse()).thenReturn(response);
|
||||
given(nativeSession.getUpgradeRequest()).willReturn(request);
|
||||
given(nativeSession.getUpgradeResponse()).willReturn(response);
|
||||
|
||||
JettyWebSocketSession session = new JettyWebSocketSession(attributes);
|
||||
session.initializeNativeSession(nativeSession);
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link org.springframework.web.socket.adapter.standard.StandardWebSocketHandlerAdapter}.
|
||||
@@ -55,13 +55,13 @@ public class StandardWebSocketHandlerAdapterTests {
|
||||
|
||||
@Test
|
||||
public void onOpen() throws Throwable {
|
||||
when(this.session.getId()).thenReturn("123");
|
||||
given(this.session.getId()).willReturn("123");
|
||||
this.adapter.onOpen(this.session, null);
|
||||
|
||||
verify(this.webSocketHandler).afterConnectionEstablished(this.webSocketSession);
|
||||
verify(this.session, atLeast(2)).addMessageHandler(any(MessageHandler.Whole.class));
|
||||
|
||||
when(this.session.getId()).thenReturn("123");
|
||||
given(this.session.getId()).willReturn("123");
|
||||
assertEquals("123", this.webSocketSession.getId());
|
||||
}
|
||||
|
||||
|
||||
@@ -15,22 +15,19 @@
|
||||
|
||||
package org.springframework.web.socket.adapter.standard;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.websocket.Session;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mockito;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.web.socket.handler.TestPrincipal;
|
||||
|
||||
import javax.websocket.Session;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertSame;
|
||||
import static org.mockito.Mockito.reset;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.web.socket.adapter.standard.StandardWebSocketSession}.
|
||||
@@ -65,7 +62,7 @@ public class StandardWebSocketSessionTests {
|
||||
TestPrincipal user = new TestPrincipal("joe");
|
||||
|
||||
Session nativeSession = Mockito.mock(Session.class);
|
||||
when(nativeSession.getUserPrincipal()).thenReturn(user);
|
||||
given(nativeSession.getUserPrincipal()).willReturn(user);
|
||||
|
||||
StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null);
|
||||
session.initializeNativeSession(nativeSession);
|
||||
@@ -77,7 +74,7 @@ public class StandardWebSocketSessionTests {
|
||||
public void getPrincipalNone() {
|
||||
|
||||
Session nativeSession = Mockito.mock(Session.class);
|
||||
when(nativeSession.getUserPrincipal()).thenReturn(null);
|
||||
given(nativeSession.getUserPrincipal()).willReturn(null);
|
||||
|
||||
StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null);
|
||||
session.initializeNativeSession(nativeSession);
|
||||
@@ -94,7 +91,7 @@ public class StandardWebSocketSessionTests {
|
||||
String protocol = "foo";
|
||||
|
||||
Session nativeSession = Mockito.mock(Session.class);
|
||||
when(nativeSession.getNegotiatedSubprotocol()).thenReturn(protocol);
|
||||
given(nativeSession.getNegotiatedSubprotocol()).willReturn(protocol);
|
||||
|
||||
StandardWebSocketSession session = new StandardWebSocketSession(this.headers, this.attributes, null, null);
|
||||
session.initializeNativeSession(nativeSession);
|
||||
|
||||
@@ -23,7 +23,7 @@ import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link ExceptionWebSocketHandlerDecorator}.
|
||||
@@ -52,8 +52,8 @@ public class ExceptionWebSocketHandlerDecoratorTests {
|
||||
@Test
|
||||
public void afterConnectionEstablished() throws Exception {
|
||||
|
||||
doThrow(new IllegalStateException("error"))
|
||||
.when(this.delegate).afterConnectionEstablished(this.session);
|
||||
willThrow(new IllegalStateException("error"))
|
||||
.given(this.delegate).afterConnectionEstablished(this.session);
|
||||
|
||||
this.decorator.afterConnectionEstablished(this.session);
|
||||
|
||||
@@ -65,8 +65,8 @@ public class ExceptionWebSocketHandlerDecoratorTests {
|
||||
|
||||
TextMessage message = new TextMessage("payload");
|
||||
|
||||
doThrow(new IllegalStateException("error"))
|
||||
.when(this.delegate).handleMessage(this.session, message);
|
||||
willThrow(new IllegalStateException("error"))
|
||||
.given(this.delegate).handleMessage(this.session, message);
|
||||
|
||||
this.decorator.handleMessage(this.session, message);
|
||||
|
||||
@@ -78,8 +78,8 @@ public class ExceptionWebSocketHandlerDecoratorTests {
|
||||
|
||||
Exception exception = new Exception("transport error");
|
||||
|
||||
doThrow(new IllegalStateException("error"))
|
||||
.when(this.delegate).handleTransportError(this.session, exception);
|
||||
willThrow(new IllegalStateException("error"))
|
||||
.given(this.delegate).handleTransportError(this.session, exception);
|
||||
|
||||
this.decorator.handleTransportError(this.session, exception);
|
||||
|
||||
@@ -91,12 +91,12 @@ public class ExceptionWebSocketHandlerDecoratorTests {
|
||||
|
||||
CloseStatus closeStatus = CloseStatus.NORMAL;
|
||||
|
||||
doThrow(new IllegalStateException("error"))
|
||||
.when(this.delegate).afterConnectionClosed(this.session, closeStatus);
|
||||
willThrow(new IllegalStateException("error"))
|
||||
.given(this.delegate).afterConnectionClosed(this.session, closeStatus);
|
||||
|
||||
this.decorator.afterConnectionClosed(this.session, closeStatus);
|
||||
|
||||
assertNull(this.session.getCloseStatus());
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -31,11 +31,8 @@ import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.handler.ConcurrentWebSocketSessionDecorator;
|
||||
import org.springframework.web.socket.handler.TestWebSocketSession;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link SubProtocolWebSocketHandler}.
|
||||
@@ -65,8 +62,8 @@ public class SubProtocolWebSocketHandlerTests {
|
||||
public void setup() {
|
||||
MockitoAnnotations.initMocks(this);
|
||||
this.webSocketHandler = new SubProtocolWebSocketHandler(this.inClientChannel, this.outClientChannel);
|
||||
when(stompHandler.getSupportedProtocols()).thenReturn(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"));
|
||||
when(mqttHandler.getSupportedProtocols()).thenReturn(Arrays.asList("MQTT"));
|
||||
given(stompHandler.getSupportedProtocols()).willReturn(Arrays.asList("v10.stomp", "v11.stomp", "v12.stomp"));
|
||||
given(mqttHandler.getSupportedProtocols()).willReturn(Arrays.asList("MQTT"));
|
||||
this.session = new TestWebSocketSession();
|
||||
this.session.setId("1");
|
||||
}
|
||||
|
||||
@@ -26,14 +26,14 @@ import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
import org.springframework.web.socket.AbstractHttpRequestTests;
|
||||
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
|
||||
import org.springframework.web.socket.SubProtocolCapable;
|
||||
import org.springframework.web.socket.WebSocketExtension;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
import org.springframework.web.socket.WebSocketHttpHeaders;
|
||||
import org.springframework.web.socket.handler.TextWebSocketHandler;
|
||||
import org.springframework.web.socket.server.support.DefaultHandshakeHandler;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link org.springframework.web.socket.server.support.DefaultHandshakeHandler}.
|
||||
@@ -60,7 +60,7 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests {
|
||||
|
||||
this.handshakeHandler.setSupportedProtocols("stomp", "mqtt");
|
||||
|
||||
when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[] {"13"});
|
||||
given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
|
||||
|
||||
this.servletRequest.setMethod("GET");
|
||||
|
||||
@@ -86,8 +86,8 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests {
|
||||
WebSocketExtension extension1 = new WebSocketExtension("ext1");
|
||||
WebSocketExtension extension2 = new WebSocketExtension("ext2");
|
||||
|
||||
when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[] {"13"});
|
||||
when(this.upgradeStrategy.getSupportedExtensions(this.request)).thenReturn(Arrays.asList(extension1));
|
||||
given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[] {"13"});
|
||||
given(this.upgradeStrategy.getSupportedExtensions(this.request)).willReturn(Arrays.asList(extension1));
|
||||
|
||||
this.servletRequest.setMethod("GET");
|
||||
|
||||
@@ -109,7 +109,7 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests {
|
||||
@Test
|
||||
public void subProtocolCapableHandler() throws Exception {
|
||||
|
||||
when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[]{"13"});
|
||||
given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[]{"13"});
|
||||
|
||||
this.servletRequest.setMethod("GET");
|
||||
|
||||
@@ -131,7 +131,7 @@ public class DefaultHandshakeHandlerTests extends AbstractHttpRequestTests {
|
||||
@Test
|
||||
public void subProtocolCapableHandlerNoMatch() throws Exception {
|
||||
|
||||
when(this.upgradeStrategy.getSupportedVersions()).thenReturn(new String[]{"13"});
|
||||
given(this.upgradeStrategy.getSupportedVersions()).willReturn(new String[]{"13"});
|
||||
|
||||
this.servletRequest.setMethod("GET");
|
||||
|
||||
|
||||
@@ -27,7 +27,7 @@ import org.springframework.web.socket.AbstractHttpRequestTests;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.server.HandshakeInterceptor;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link HandshakeInterceptorChain}.
|
||||
@@ -62,9 +62,9 @@ public class HandshakeInterceptorChainTests extends AbstractHttpRequestTests {
|
||||
|
||||
@Test
|
||||
public void success() throws Exception {
|
||||
when(i1.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(true);
|
||||
when(i2.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(true);
|
||||
when(i3.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(true);
|
||||
given(i1.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true);
|
||||
given(i2.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true);
|
||||
given(i3.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true);
|
||||
|
||||
HandshakeInterceptorChain chain = new HandshakeInterceptorChain(interceptors, wsHandler);
|
||||
chain.applyBeforeHandshake(request, response, attributes);
|
||||
@@ -77,8 +77,8 @@ public class HandshakeInterceptorChainTests extends AbstractHttpRequestTests {
|
||||
|
||||
@Test
|
||||
public void applyBeforeHandshakeWithFalseReturnValue() throws Exception {
|
||||
when(i1.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(true);
|
||||
when(i2.beforeHandshake(request, response, wsHandler, attributes)).thenReturn(false);
|
||||
given(i1.beforeHandshake(request, response, wsHandler, attributes)).willReturn(true);
|
||||
given(i2.beforeHandshake(request, response, wsHandler, attributes)).willReturn(false);
|
||||
|
||||
HandshakeInterceptorChain chain = new HandshakeInterceptorChain(interceptors, wsHandler);
|
||||
chain.applyBeforeHandshake(request, response, attributes);
|
||||
|
||||
@@ -16,6 +16,11 @@
|
||||
|
||||
package org.springframework.web.socket.sockjs.client;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
@@ -30,15 +35,9 @@ import org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec;
|
||||
import org.springframework.web.socket.sockjs.frame.SockJsFrame;
|
||||
import org.springframework.web.socket.sockjs.transport.TransportType;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.net.InetSocketAddress;
|
||||
import java.net.URI;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertThat;
|
||||
import static org.hamcrest.CoreMatchers.*;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for
|
||||
@@ -47,7 +46,7 @@ import static org.mockito.Mockito.*;
|
||||
* @author Rossen Stoyanchev
|
||||
*/
|
||||
public class ClientSockJsSessionTests {
|
||||
|
||||
|
||||
private static final Jackson2SockJsMessageCodec CODEC = new Jackson2SockJsMessageCodec();
|
||||
|
||||
private TestClientSockJsSession session;
|
||||
@@ -92,7 +91,7 @@ public class ClientSockJsSessionTests {
|
||||
|
||||
@Test
|
||||
public void handleFrameOpenWithWebSocketHandlerException() throws Exception {
|
||||
doThrow(new IllegalStateException("Fake error")).when(this.handler).afterConnectionEstablished(this.session);
|
||||
willThrow(new IllegalStateException("Fake error")).given(this.handler).afterConnectionEstablished(this.session);
|
||||
this.session.handleFrame(SockJsFrame.openFrame().getContent());
|
||||
assertThat(this.session.isOpen(), is(true));
|
||||
}
|
||||
@@ -129,8 +128,8 @@ public class ClientSockJsSessionTests {
|
||||
@Test
|
||||
public void handleFrameMessageWithWebSocketHandlerException() throws Exception {
|
||||
this.session.handleFrame(SockJsFrame.openFrame().getContent());
|
||||
doThrow(new IllegalStateException("Fake error")).when(this.handler).handleMessage(this.session, new TextMessage("foo"));
|
||||
doThrow(new IllegalStateException("Fake error")).when(this.handler).handleMessage(this.session, new TextMessage("bar"));
|
||||
willThrow(new IllegalStateException("Fake error")).given(this.handler).handleMessage(this.session, new TextMessage("foo"));
|
||||
willThrow(new IllegalStateException("Fake error")).given(this.handler).handleMessage(this.session, new TextMessage("bar"));
|
||||
this.session.handleFrame(SockJsFrame.messageFrame(CODEC, "foo", "bar").getContent());
|
||||
assertThat(this.session.isOpen(), equalTo(true));
|
||||
verify(this.handler).afterConnectionEstablished(this.session);
|
||||
|
||||
@@ -16,6 +16,16 @@
|
||||
|
||||
package org.springframework.web.socket.sockjs.client;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.core.task.SyncTaskExecutor;
|
||||
@@ -45,17 +55,7 @@ import org.springframework.web.socket.sockjs.frame.Jackson2SockJsMessageCodec;
|
||||
import org.springframework.web.socket.sockjs.frame.SockJsFrame;
|
||||
import org.springframework.web.socket.sockjs.transport.TransportType;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.net.URI;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.util.Queue;
|
||||
import java.util.concurrent.CountDownLatch;
|
||||
import java.util.concurrent.LinkedBlockingDeque;
|
||||
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link RestTemplateXhrTransport}.
|
||||
@@ -127,7 +127,7 @@ public class RestTemplateXhrTransportTests {
|
||||
public void connectFailure() throws Exception {
|
||||
final HttpServerErrorException expected = new HttpServerErrorException(HttpStatus.INTERNAL_SERVER_ERROR);
|
||||
RestOperations restTemplate = mock(RestOperations.class);
|
||||
when(restTemplate.execute(any(), eq(HttpMethod.POST), any(), any())).thenThrow(expected);
|
||||
given(restTemplate.execute((URI) any(), eq(HttpMethod.POST), any(), any())).willThrow(expected);
|
||||
|
||||
final CountDownLatch latch = new CountDownLatch(1);
|
||||
connect(restTemplate).addCallback(
|
||||
@@ -189,8 +189,8 @@ public class RestTemplateXhrTransportTests {
|
||||
private ClientHttpResponse response(HttpStatus status, String body) throws IOException {
|
||||
ClientHttpResponse response = mock(ClientHttpResponse.class);
|
||||
InputStream inputStream = getInputStream(body);
|
||||
when(response.getStatusCode()).thenReturn(status);
|
||||
when(response.getBody()).thenReturn(inputStream);
|
||||
given(response.getStatusCode()).willReturn(status);
|
||||
given(response.getBody()).willReturn(inputStream);
|
||||
return response;
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,10 @@
|
||||
|
||||
package org.springframework.web.socket.sockjs.client;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -25,13 +29,8 @@ import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
import org.springframework.web.socket.sockjs.client.TestTransport.XhrTestTransport;
|
||||
|
||||
import java.net.URISyntaxException;
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link org.springframework.web.socket.sockjs.client.SockJsClient}.
|
||||
@@ -41,7 +40,7 @@ import static org.mockito.Mockito.*;
|
||||
public class SockJsClientTests {
|
||||
|
||||
private static final String URL = "http://example.com";
|
||||
|
||||
|
||||
private static final WebSocketHandler handler = mock(WebSocketHandler.class);
|
||||
|
||||
|
||||
@@ -122,7 +121,7 @@ public class SockJsClientTests {
|
||||
@SuppressWarnings("unchecked")
|
||||
public void connectInfoRequestFailure() throws URISyntaxException {
|
||||
HttpServerErrorException exception = new HttpServerErrorException(HttpStatus.SERVICE_UNAVAILABLE);
|
||||
when(this.infoReceiver.executeInfoRequest(any())).thenThrow(exception);
|
||||
given(this.infoReceiver.executeInfoRequest(any())).willThrow(exception);
|
||||
this.sockJsClient.doHandshake(handler, URL).addCallback(this.connectCallback);
|
||||
verify(this.connectCallback).onFailure(exception);
|
||||
assertFalse(this.webSocketTransport.invoked());
|
||||
@@ -130,7 +129,7 @@ public class SockJsClientTests {
|
||||
}
|
||||
|
||||
private void setupInfoRequest(boolean webSocketEnabled) {
|
||||
when(this.infoReceiver.executeInfoRequest(any())).thenReturn("{\"entropy\":123," +
|
||||
given(this.infoReceiver.executeInfoRequest(any())).willReturn("{\"entropy\":123," +
|
||||
"\"origins\":[\"*:*\"],\"cookie_needed\":true,\"websocket\":" + webSocketEnabled + "}");
|
||||
}
|
||||
|
||||
|
||||
@@ -16,6 +16,8 @@
|
||||
|
||||
package org.springframework.web.socket.sockjs.client;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.mockito.ArgumentCaptor;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
@@ -28,15 +30,8 @@ import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.WebSocketSession;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.verifyNoMoreInteractions;
|
||||
import static org.mockito.Mockito.when;
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Unit tests for
|
||||
@@ -88,8 +83,8 @@ public class XhrTransportTests {
|
||||
handshakeHeaders.setOrigin("foo");
|
||||
|
||||
TransportRequest request = mock(TransportRequest.class);
|
||||
when(request.getSockJsUrlInfo()).thenReturn(new SockJsUrlInfo(new URI("http://example.com")));
|
||||
when(request.getHandshakeHeaders()).thenReturn(handshakeHeaders);
|
||||
given(request.getSockJsUrlInfo()).willReturn(new SockJsUrlInfo(new URI("http://example.com")));
|
||||
given(request.getHandshakeHeaders()).willReturn(handshakeHeaders);
|
||||
|
||||
HttpHeaders requestHeaders = new HttpHeaders();
|
||||
requestHeaders.set("foo", "bar");
|
||||
|
||||
@@ -18,6 +18,9 @@ package org.springframework.web.socket.sockjs.support;
|
||||
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -31,10 +34,7 @@ import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.sockjs.SockJsException;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
|
||||
import javax.servlet.ServletOutputStream;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link AbstractSockJsService}.
|
||||
@@ -117,8 +117,8 @@ public class SockJsServiceTests extends AbstractHttpRequestTests {
|
||||
public void handleInfoGetWildflyNPE() throws Exception {
|
||||
HttpServletResponse mockResponse = mock(HttpServletResponse.class);
|
||||
ServletOutputStream ous = mock(ServletOutputStream.class);
|
||||
when(mockResponse.getHeaders("Access-Control-Allow-Origin")).thenThrow(NullPointerException.class);
|
||||
when(mockResponse.getOutputStream()).thenReturn(ous);
|
||||
given(mockResponse.getHeaders("Access-Control-Allow-Origin")).willThrow(NullPointerException.class);
|
||||
given(mockResponse.getOutputStream()).willReturn(ous);
|
||||
this.response = new ServletServerHttpResponse(mockResponse);
|
||||
|
||||
handleRequest("GET", "/echo/info", HttpStatus.OK);
|
||||
|
||||
@@ -23,7 +23,6 @@ import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.mockito.Mock;
|
||||
import org.mockito.MockitoAnnotations;
|
||||
|
||||
import org.springframework.scheduling.TaskScheduler;
|
||||
import org.springframework.web.socket.AbstractHttpRequestTests;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
@@ -35,7 +34,7 @@ import org.springframework.web.socket.sockjs.transport.session.StubSockJsService
|
||||
import org.springframework.web.socket.sockjs.transport.session.TestSockJsSession;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link org.springframework.web.socket.sockjs.transport.handler.DefaultSockJsService}.
|
||||
@@ -72,9 +71,9 @@ public class DefaultSockJsServiceTests extends AbstractHttpRequestTests {
|
||||
Map<String, Object> attributes = Collections.emptyMap();
|
||||
this.session = new TestSockJsSession(sessionId, new StubSockJsServiceConfig(), this.wsHandler, attributes);
|
||||
|
||||
when(this.xhrHandler.getTransportType()).thenReturn(TransportType.XHR);
|
||||
when(this.xhrHandler.createSession(sessionId, this.wsHandler, attributes)).thenReturn(this.session);
|
||||
when(this.xhrSendHandler.getTransportType()).thenReturn(TransportType.XHR_SEND);
|
||||
given(this.xhrHandler.getTransportType()).willReturn(TransportType.XHR);
|
||||
given(this.xhrHandler.createSession(sessionId, this.wsHandler, attributes)).willReturn(this.session);
|
||||
given(this.xhrSendHandler.getTransportType()).willReturn(TransportType.XHR_SEND);
|
||||
|
||||
this.service = new TransportHandlingSockJsService(this.taskScheduler, this.xhrHandler, this.xhrSendHandler);
|
||||
}
|
||||
|
||||
@@ -28,7 +28,7 @@ import org.springframework.web.socket.sockjs.transport.session.StubSockJsService
|
||||
import org.springframework.web.socket.sockjs.transport.session.TestHttpSockJsSession;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link AbstractHttpReceivingTransportHandler} and sub-classes
|
||||
@@ -110,7 +110,7 @@ public class HttpReceivingTransportHandlerTests extends AbstractHttpRequestTest
|
||||
TestHttpSockJsSession session = new TestHttpSockJsSession("1", sockJsConfig, wsHandler, null);
|
||||
session.delegateConnectionEstablished();
|
||||
|
||||
doThrow(new Exception()).when(wsHandler).handleMessage(session, new TextMessage("x"));
|
||||
willThrow(new Exception()).given(wsHandler).handleMessage(session, new TextMessage("x"));
|
||||
|
||||
try {
|
||||
XhrReceivingTransportHandler transportHandler = new XhrReceivingTransportHandler();
|
||||
|
||||
@@ -26,13 +26,13 @@ import org.junit.Test;
|
||||
import org.springframework.web.socket.CloseStatus;
|
||||
import org.springframework.web.socket.TextMessage;
|
||||
import org.springframework.web.socket.WebSocketHandler;
|
||||
import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator;
|
||||
import org.springframework.web.socket.sockjs.SockJsMessageDeliveryException;
|
||||
import org.springframework.web.socket.sockjs.SockJsTransportFailureException;
|
||||
import org.springframework.web.socket.sockjs.frame.SockJsFrame;
|
||||
import org.springframework.web.socket.handler.ExceptionWebSocketHandlerDecorator;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
import static org.mockito.Mockito.*;
|
||||
import static org.mockito.BDDMockito.*;
|
||||
|
||||
/**
|
||||
* Test fixture for {@link AbstractSockJsSession}.
|
||||
@@ -108,7 +108,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
|
||||
String msg2 = "message 2";
|
||||
String msg3 = "message 3";
|
||||
|
||||
doThrow(new IOException()).when(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
|
||||
willThrow(new IOException()).given(this.webSocketHandler).handleMessage(sockJsSession, new TextMessage(msg2));
|
||||
|
||||
sockJsSession.delegateConnectionEstablished();
|
||||
try {
|
||||
@@ -203,7 +203,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
|
||||
@Test
|
||||
public void closeWithWebSocketHandlerExceptions() throws Exception {
|
||||
|
||||
doThrow(new Exception()).when(this.webSocketHandler).afterConnectionClosed(this.session, CloseStatus.NORMAL);
|
||||
willThrow(new Exception()).given(this.webSocketHandler).afterConnectionClosed(this.session, CloseStatus.NORMAL);
|
||||
|
||||
this.session.delegateConnectionEstablished();
|
||||
this.session.setActive(true);
|
||||
@@ -278,7 +278,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
|
||||
public void scheduleAndCancelHeartbeat() throws Exception {
|
||||
|
||||
ScheduledFuture<?> task = mock(ScheduledFuture.class);
|
||||
doReturn(task).when(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
|
||||
willReturn(task).given(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
|
||||
|
||||
this.session.setActive(true);
|
||||
this.session.scheduleHeartbeat();
|
||||
@@ -286,7 +286,7 @@ public class SockJsSessionTests extends AbstractSockJsSessionTests<TestSockJsSes
|
||||
verify(this.taskScheduler).schedule(any(Runnable.class), any(Date.class));
|
||||
verifyNoMoreInteractions(this.taskScheduler);
|
||||
|
||||
doReturn(false).when(task).isDone();
|
||||
given(task.isDone()).willReturn(false);
|
||||
|
||||
this.session.cancelHeartbeat();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user