Migrate to Mockito.mock(T...) where feasible

This commit is contained in:
Sam Brannen
2023-01-19 14:32:29 +01:00
parent c3d123fef7
commit c4c786596f
369 changed files with 2267 additions and 2707 deletions

View File

@@ -18,7 +18,6 @@ package org.springframework.messaging.converter;
import java.util.Map;
import com.google.protobuf.ExtensionRegistry;
import org.junit.jupiter.api.Test;
import org.skyscreamer.jsonassert.JSONAssert;
@@ -108,7 +107,7 @@ class ProtobufMessageConverterTests {
void jsonWithGoogleProtobuf() throws Exception {
ProtobufMessageConverter converter = new ProtobufMessageConverter(
new ProtobufMessageConverter.ProtobufJavaUtilSupport(null, null),
mock(ExtensionRegistry.class));
mock());
//convertTo
Message<?> message = converter.toMessage(testMsg, new MessageHeaders(Map.of(CONTENT_TYPE, APPLICATION_JSON)));

View File

@@ -36,7 +36,7 @@ public class CachingDestinationResolverTests {
@Test
public void cachedDestination() {
@SuppressWarnings("unchecked")
DestinationResolver<String> resolver = mock(DestinationResolver.class);
DestinationResolver<String> resolver = mock();
CachingDestinationResolverProxy<String> resolverProxy = new CachingDestinationResolverProxy<>(resolver);
given(resolver.resolveDestination("abcd")).willReturn("dcba");

View File

@@ -72,7 +72,7 @@ public class GenericMessagingTemplateTests {
@Test
public void sendWithTimeout() {
SubscribableChannel channel = mock(SubscribableChannel.class);
SubscribableChannel channel = mock();
final AtomicReference<Message<?>> sent = new AtomicReference<>();
willAnswer(invocation -> {
sent.set(invocation.getArgument(0));
@@ -91,7 +91,7 @@ public class GenericMessagingTemplateTests {
@Test
public void sendWithTimeoutMutable() {
SubscribableChannel channel = mock(SubscribableChannel.class);
SubscribableChannel channel = mock();
final AtomicReference<Message<?>> sent = new AtomicReference<>();
willAnswer(invocation -> {
sent.set(invocation.getArgument(0));
@@ -129,7 +129,7 @@ public class GenericMessagingTemplateTests {
this.template.setSendTimeout(30_000L);
this.template.setThrowExceptionOnLateReply(true);
SubscribableChannel channel = mock(SubscribableChannel.class);
SubscribableChannel channel = mock();
MessageHandler handler = createLateReplier(latch, failure);
willAnswer(invocation -> {
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));
@@ -155,7 +155,7 @@ public class GenericMessagingTemplateTests {
this.template.setReceiveTimeout(10_000);
this.template.setThrowExceptionOnLateReply(true);
SubscribableChannel channel = mock(SubscribableChannel.class);
SubscribableChannel channel = mock();
MessageHandler handler = createLateReplier(latch, failure);
willAnswer(invocation -> {
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));
@@ -187,7 +187,7 @@ public class GenericMessagingTemplateTests {
this.template.setSendTimeoutHeader("sto");
this.template.setReceiveTimeoutHeader("rto");
SubscribableChannel channel = mock(SubscribableChannel.class);
SubscribableChannel channel = mock();
MessageHandler handler = createLateReplier(latch, failure);
willAnswer(invocation -> {
this.executor.execute(() -> handler.handleMessage(invocation.getArgument(0)));

View File

@@ -44,20 +44,17 @@ import static org.mockito.Mockito.mock;
*/
public class MessageMethodArgumentResolverTests {
private MessageConverter converter;
private MessageConverter converter = mock();
private MessageMethodArgumentResolver resolver;
private MessageMethodArgumentResolver resolver = new MessageMethodArgumentResolver(this.converter);
private Method method;
@BeforeEach
public void setup() throws Exception {
this.method = MessageMethodArgumentResolverTests.class.getDeclaredMethod("handle",
this.method = getClass().getDeclaredMethod("handle",
Message.class, Message.class, Message.class, Message.class, ErrorMessage.class, Message.class);
this.converter = mock(MessageConverter.class);
this.resolver = new MessageMethodArgumentResolver(this.converter);
}

View File

@@ -39,7 +39,7 @@ import static org.mockito.Mockito.mock;
*/
public class InvocableHandlerMethodTests {
private final Message<?> message = mock(Message.class);
private final Message<?> message = mock();
private final HandlerMethodArgumentResolverComposite resolvers = new HandlerMethodArgumentResolverComposite();

View File

@@ -46,7 +46,7 @@ import static org.mockito.Mockito.mock;
*/
public class InvocableHandlerMethodTests {
private final Message<?> message = mock(Message.class);
private final Message<?> message = mock();
private final List<HandlerMethodArgumentResolver> resolvers = new ArrayList<>();

View File

@@ -66,7 +66,7 @@ import static org.mockito.Mockito.verify;
*/
public class DefaultRSocketRequesterBuilderTests {
private ClientTransport transport;
private ClientTransport transport = mock();
private final MockConnection connection = new MockConnection();
@@ -75,7 +75,6 @@ public class DefaultRSocketRequesterBuilderTests {
@BeforeEach
public void setup() {
this.transport = mock(ClientTransport.class);
given(this.transport.connect()).willReturn(Mono.just(this.connection));
given(this.transport.maxFrameLength()).willReturn(16777215);
}
@@ -84,7 +83,7 @@ public class DefaultRSocketRequesterBuilderTests {
@Test
@SuppressWarnings("unchecked")
public void rsocketConnectorConfigurer() {
Consumer<RSocketStrategies.Builder> strategiesConfigurer = mock(Consumer.class);
Consumer<RSocketStrategies.Builder> strategiesConfigurer = mock();
RSocketRequester.builder()
.rsocketConnector(this.connectorConfigurer)
.rsocketStrategies(strategiesConfigurer)

View File

@@ -110,7 +110,7 @@ class DefaultRSocketStrategiesTests {
@Test
@SuppressWarnings("unchecked")
void applyMetadataExtractors() {
Consumer<MetadataExtractorRegistry> consumer = mock(Consumer.class);
Consumer<MetadataExtractorRegistry> consumer = mock();
RSocketStrategies.builder().metadataExtractorRegistry(consumer).build();
verify(consumer, times(1)).accept(any());
}

View File

@@ -60,7 +60,7 @@ public class SimpAttributesTests {
@Test
public void registerDestructionCallback() {
Runnable callback = mock(Runnable.class);
Runnable callback = mock();
this.simpAttributes.registerDestructionCallback("name1", callback);
assertThat(this.simpAttributes.getAttribute(
@@ -70,15 +70,15 @@ public class SimpAttributesTests {
@Test
public void registerDestructionCallbackAfterSessionCompleted() {
this.simpAttributes.sessionCompleted();
assertThatIllegalStateException().isThrownBy(() ->
this.simpAttributes.registerDestructionCallback("name1", mock(Runnable.class)))
.withMessageContaining("already completed");
assertThatIllegalStateException()
.isThrownBy(() -> this.simpAttributes.registerDestructionCallback("name1", mock()))
.withMessageContaining("already completed");
}
@Test
public void removeDestructionCallback() {
Runnable callback1 = mock(Runnable.class);
Runnable callback2 = mock(Runnable.class);
Runnable callback1 = mock();
Runnable callback2 = mock();
this.simpAttributes.registerDestructionCallback("name1", callback1);
this.simpAttributes.registerDestructionCallback("name2", callback2);
@@ -100,8 +100,8 @@ public class SimpAttributesTests {
@Test
public void sessionCompleted() {
Runnable callback1 = mock(Runnable.class);
Runnable callback2 = mock(Runnable.class);
Runnable callback1 = mock();
Runnable callback2 = mock();
this.simpAttributes.registerDestructionCallback("name1", callback1);
this.simpAttributes.registerDestructionCallback("name2", callback2);
@@ -113,7 +113,7 @@ public class SimpAttributesTests {
@Test
public void sessionCompletedIsIdempotent() {
Runnable callback1 = mock(Runnable.class);
Runnable callback1 = mock();
this.simpAttributes.registerDestructionCallback("name1", callback1);
this.simpAttributes.sessionCompleted();

View File

@@ -77,11 +77,11 @@ public class SimpMessageHeaderAccessorTests {
SimpMessageHeaderAccessor accessor = SimpMessageHeaderAccessor.create();
accessor.setUserChangeCallback(userCallback);
Principal user1 = mock(Principal.class);
Principal user1 = mock();
accessor.setUser(user1);
assertThat(userCallback.getUser()).isEqualTo(user1);
Principal user2 = mock(Principal.class);
Principal user2 = mock();
accessor.setUser(user2);
assertThat(userCallback.getUser()).isEqualTo(user2);
}

View File

@@ -21,12 +21,12 @@ import java.util.concurrent.ConcurrentHashMap;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.ObjectFactory;
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.times;
import static org.mockito.Mockito.verify;
@@ -38,19 +38,16 @@ import static org.mockito.Mockito.verify;
*/
public class SimpSessionScopeTests {
private SimpSessionScope scope;
private SimpSessionScope scope = new SimpSessionScope();
@SuppressWarnings("rawtypes")
private ObjectFactory objectFactory;
private ObjectFactory objectFactory = mock();
private SimpAttributes simpAttributes;
private SimpAttributes simpAttributes = new SimpAttributes("session1", new ConcurrentHashMap<>());
@BeforeEach
public void setUp() {
this.scope = new SimpSessionScope();
this.objectFactory = Mockito.mock(ObjectFactory.class);
this.simpAttributes = new SimpAttributes("session1", new ConcurrentHashMap<>());
SimpAttributesContextHolder.setAttributes(this.simpAttributes);
}
@@ -90,7 +87,7 @@ public class SimpSessionScopeTests {
@Test
public void registerDestructionCallback() {
Runnable runnable = Mockito.mock(Runnable.class);
Runnable runnable = mock();
this.scope.registerDestructionCallback("name", runnable);
this.simpAttributes.sessionCompleted();
@@ -102,5 +99,4 @@ public class SimpSessionScopeTests {
assertThat(this.scope.getConversationId()).isEqualTo("session1");
}
}

View File

@@ -322,7 +322,7 @@ public class SendToMethodReturnValueHandlerTests {
public void testHeadersToSend() throws Exception {
Message<?> message = createMessage("sess1", "sub1", "/app", "/dest", null);
SimpMessageSendingOperations messagingTemplate = mock(SimpMessageSendingOperations.class);
SimpMessageSendingOperations messagingTemplate = mock();
SendToMethodReturnValueHandler handler = new SendToMethodReturnValueHandler(messagingTemplate, false);
handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, message);

View File

@@ -150,7 +150,7 @@ public class SubscriptionMethodReturnValueHandlerTests {
String destination = "/dest";
Message<?> inputMessage = createInputMessage(sessionId, subscriptionId, destination, null);
MessageSendingOperations messagingTemplate = mock(MessageSendingOperations.class);
MessageSendingOperations messagingTemplate = mock();
SubscriptionMethodReturnValueHandler handler = new SubscriptionMethodReturnValueHandler(messagingTemplate);
handler.handleReturnValue(PAYLOAD, this.subscribeEventReturnType, inputMessage);

View File

@@ -26,8 +26,6 @@ import org.junit.jupiter.api.Test;
import org.springframework.context.ApplicationEvent;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
import org.springframework.messaging.SubscribableChannel;
import org.springframework.messaging.simp.SimpMessageHeaderAccessor;
import org.springframework.messaging.simp.SimpMessageType;
import org.springframework.messaging.support.GenericMessage;
@@ -165,8 +163,7 @@ public class BrokerMessageHandlerTests {
TestBrokerMessageHandler(String... destinationPrefixes) {
super(mock(SubscribableChannel.class), mock(MessageChannel.class),
mock(SubscribableChannel.class), Arrays.asList(destinationPrefixes));
super(mock(), mock(), mock(), Arrays.asList(destinationPrefixes));
setApplicationEventPublisher(this);
}

View File

@@ -184,7 +184,7 @@ public class SimpleBrokerMessageHandlerTests {
@Test
@SuppressWarnings("rawtypes")
public void startAndStopWithHeartbeatValue() {
ScheduledFuture future = mock(ScheduledFuture.class);
ScheduledFuture future = mock();
given(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), eq(Duration.ofMillis(15000)))).willReturn(future);
this.messageHandler.setTaskScheduler(this.taskScheduler);

View File

@@ -314,7 +314,7 @@ public class MessageBrokerConfigurationTests {
@Test
public void configureMessageConvertersCustom() {
final MessageConverter testConverter = mock(MessageConverter.class);
final MessageConverter testConverter = mock();
AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
@Override
protected boolean configureMessageConverters(List<MessageConverter> messageConverters) {
@@ -331,7 +331,7 @@ public class MessageBrokerConfigurationTests {
@Test
public void configureMessageConvertersCustomAndDefault() {
final MessageConverter testConverter = mock(MessageConverter.class);
final MessageConverter testConverter = mock();
AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
@Override
@@ -378,7 +378,7 @@ public class MessageBrokerConfigurationTests {
@Test
public void simpValidatorCustom() {
final Validator validator = mock(Validator.class);
final Validator validator = mock();
AbstractMessageBrokerConfiguration config = new BaseTestMessageBrokerConfig() {
@Override
public Validator getValidator() {
@@ -654,12 +654,12 @@ public class MessageBrokerConfigurationTests {
@Override
protected void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers) {
argumentResolvers.add(mock(HandlerMethodArgumentResolver.class));
argumentResolvers.add(mock());
}
@Override
protected void addReturnValueHandlers(List<HandlerMethodReturnValueHandler> returnValueHandlers) {
returnValueHandlers.add(mock(HandlerMethodReturnValueHandler.class));
returnValueHandlers.add(mock());
}
@Override

View File

@@ -279,7 +279,7 @@ public class DefaultStompSessionTests {
public void handleMessageFrame() {
this.session.afterConnected(this.connection);
StompFrameHandler frameHandler = mock(StompFrameHandler.class);
StompFrameHandler frameHandler = mock();
String destination = "/topic/foo";
Subscription subscription = this.session.subscribe(destination, frameHandler);
@@ -307,7 +307,7 @@ public class DefaultStompSessionTests {
this.session.afterConnected(this.connection);
assertThat(this.session.isConnected()).isTrue();
StompFrameHandler frameHandler = mock(StompFrameHandler.class);
StompFrameHandler frameHandler = mock();
String destination = "/topic/foo";
Subscription subscription = this.session.subscribe(destination, frameHandler);
@@ -377,7 +377,7 @@ public class DefaultStompSessionTests {
this.session.afterConnected(this.connection);
assertThat(this.session.isConnected()).isTrue();
this.session.setTaskScheduler(mock(TaskScheduler.class));
this.session.setTaskScheduler(mock());
this.session.setAutoReceipt(true);
this.session.send("/topic/foo", "sample payload");
@@ -449,7 +449,7 @@ public class DefaultStompSessionTests {
assertThat(this.session.isConnected()).isTrue();
String destination = "/topic/foo";
StompFrameHandler frameHandler = mock(StompFrameHandler.class);
StompFrameHandler frameHandler = mock();
Subscription subscription = this.session.subscribe(destination, frameHandler);
Message<byte[]> message = this.messageCaptor.getValue();
@@ -473,7 +473,7 @@ public class DefaultStompSessionTests {
StompHeaders stompHeaders = new StompHeaders();
stompHeaders.setId(subscriptionId);
stompHeaders.setDestination(destination);
StompFrameHandler frameHandler = mock(StompFrameHandler.class);
StompFrameHandler frameHandler = mock();
Subscription subscription = this.session.subscribe(stompHeaders, frameHandler);
assertThat(subscription.getSubscriptionId()).isEqualTo(subscriptionId);
@@ -494,7 +494,7 @@ public class DefaultStompSessionTests {
assertThat(this.session.isConnected()).isTrue();
String destination = "/topic/foo";
StompFrameHandler frameHandler = mock(StompFrameHandler.class);
StompFrameHandler frameHandler = mock();
Subscription subscription = this.session.subscribe(destination, frameHandler);
subscription.unsubscribe();
@@ -518,7 +518,7 @@ public class DefaultStompSessionTests {
StompHeaders subscribeHeaders = new StompHeaders();
subscribeHeaders.setDestination("/topic/foo");
subscribeHeaders.set(headerName, headerValue);
StompFrameHandler frameHandler = mock(StompFrameHandler.class);
StompFrameHandler frameHandler = mock();
Subscription subscription = this.session.subscribe(subscribeHeaders, frameHandler);
StompHeaders unsubscribeHeaders = new StompHeaders();
@@ -572,7 +572,7 @@ public class DefaultStompSessionTests {
@Test
public void receiptReceived() {
this.session.afterConnected(this.connection);
this.session.setTaskScheduler(mock(TaskScheduler.class));
this.session.setTaskScheduler(mock());
AtomicReference<Boolean> received = new AtomicReference<>();
AtomicReference<StompHeaders> receivedHeaders = new AtomicReference<>();
@@ -580,7 +580,7 @@ public class DefaultStompSessionTests {
StompHeaders headers = new StompHeaders();
headers.setDestination("/topic/foo");
headers.setReceipt("my-receipt");
Subscription subscription = this.session.subscribe(headers, mock(StompFrameHandler.class));
Subscription subscription = this.session.subscribe(headers, mock());
subscription.addReceiptTask(receiptHeaders -> {
received.set(true);
receivedHeaders.set(receiptHeaders);
@@ -604,7 +604,7 @@ public class DefaultStompSessionTests {
@Test
public void receiptReceivedBeforeTaskAdded() {
this.session.afterConnected(this.connection);
this.session.setTaskScheduler(mock(TaskScheduler.class));
this.session.setTaskScheduler(mock());
AtomicReference<Boolean> received = new AtomicReference<>();
AtomicReference<StompHeaders> receivedHeaders = new AtomicReference<>();
@@ -612,7 +612,7 @@ public class DefaultStompSessionTests {
StompHeaders headers = new StompHeaders();
headers.setDestination("/topic/foo");
headers.setReceipt("my-receipt");
Subscription subscription = this.session.subscribe(headers, mock(StompFrameHandler.class));
Subscription subscription = this.session.subscribe(headers, mock());
StompHeaderAccessor accessor = StompHeaderAccessor.create(StompCommand.RECEIPT);
accessor.setReceiptId("my-receipt");
@@ -635,14 +635,14 @@ public class DefaultStompSessionTests {
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void receiptNotReceived() {
TaskScheduler taskScheduler = mock(TaskScheduler.class);
TaskScheduler taskScheduler = mock();
this.session.afterConnected(this.connection);
this.session.setTaskScheduler(taskScheduler);
AtomicReference<Boolean> notReceived = new AtomicReference<>();
ScheduledFuture future = mock(ScheduledFuture.class);
ScheduledFuture future = mock();
given(taskScheduler.schedule(any(Runnable.class), any(Instant.class))).willReturn(future);
StompHeaders headers = new StompHeaders();

View File

@@ -39,7 +39,6 @@ import org.springframework.messaging.tcp.ReconnectStrategy;
import org.springframework.messaging.tcp.TcpConnection;
import org.springframework.messaging.tcp.TcpConnectionHandler;
import org.springframework.messaging.tcp.TcpOperations;
import org.springframework.scheduling.TaskScheduler;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.eq;
@@ -55,18 +54,15 @@ class StompBrokerRelayMessageHandlerTests {
private StompBrokerRelayMessageHandler brokerRelay;
private StubMessageChannel outboundChannel;
private StubMessageChannel outboundChannel = new StubMessageChannel();
private StubTcpOperations tcpClient;
private StubTcpOperations tcpClient = new StubTcpOperations();
ArgumentCaptor<Runnable> messageCountTaskCaptor = ArgumentCaptor.forClass(Runnable.class);
private ArgumentCaptor<Runnable> messageCountTaskCaptor = ArgumentCaptor.forClass(Runnable.class);
@BeforeEach
void setup() {
this.outboundChannel = new StubMessageChannel();
this.brokerRelay = new StompBrokerRelayMessageHandler(new StubMessageChannel(),
this.outboundChannel, new StubMessageChannel(), Collections.singletonList("/topic")) {
@@ -77,16 +73,13 @@ class StompBrokerRelayMessageHandlerTests {
}
};
this.tcpClient = new StubTcpOperations();
this.brokerRelay.setTcpClient(this.tcpClient);
this.brokerRelay.setTaskScheduler(mock(TaskScheduler.class));
this.brokerRelay.setTaskScheduler(mock());
}
@Test
void virtualHost() {
this.brokerRelay.setVirtualHost("ABC");
this.brokerRelay.start();
@@ -107,7 +100,6 @@ class StompBrokerRelayMessageHandlerTests {
@Test
void loginAndPasscode() {
this.brokerRelay.setSystemLogin("syslogin");
this.brokerRelay.setSystemPasscode("syspasscode");
this.brokerRelay.setClientLogin("clientlogin");
@@ -180,7 +172,6 @@ class StompBrokerRelayMessageHandlerTests {
@Test
void messageFromBrokerIsEnriched() {
this.brokerRelay.start();
this.brokerRelay.handleMessage(connectMessage("sess1", "joe"));
@@ -200,7 +191,6 @@ class StompBrokerRelayMessageHandlerTests {
@Test
void connectWhenBrokerNotAvailable() {
this.brokerRelay.start();
this.brokerRelay.stopInternal();
this.brokerRelay.handleMessage(connectMessage("sess1", "joe"));
@@ -215,7 +205,6 @@ class StompBrokerRelayMessageHandlerTests {
@Test
void sendAfterBrokerUnavailable() {
this.brokerRelay.start();
assertThat(this.brokerRelay.getConnectionCount()).isEqualTo(1);
@@ -237,8 +226,7 @@ class StompBrokerRelayMessageHandlerTests {
@Test
@SuppressWarnings("rawtypes")
void systemSubscription() {
MessageHandler handler = mock(MessageHandler.class);
MessageHandler handler = mock();
this.brokerRelay.setSystemSubscriptions(Collections.singletonMap("/topic/foo", handler));
this.brokerRelay.start();
@@ -262,7 +250,6 @@ class StompBrokerRelayMessageHandlerTests {
@Test
void alreadyConnected() {
this.brokerRelay.start();
Message<byte[]> connect = connectMessage("sess1", "joe");

View File

@@ -34,16 +34,15 @@ import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.mock;
/**
* Unit tests for
* {@link org.springframework.messaging.simp.user.DefaultUserDestinationResolver}.
* Unit tests for {@link DefaultUserDestinationResolver}.
*
* @author Rossen Stoyanchev
*/
public class DefaultUserDestinationResolverTests {
private DefaultUserDestinationResolver resolver;
private SimpUserRegistry registry = mock();
private SimpUserRegistry registry;
private DefaultUserDestinationResolver resolver = new DefaultUserDestinationResolver(this.registry);
@BeforeEach
@@ -51,10 +50,7 @@ public class DefaultUserDestinationResolverTests {
TestSimpUser simpUser = new TestSimpUser("joe");
simpUser.addSessions(new TestSimpSession("123"));
this.registry = mock(SimpUserRegistry.class);
given(this.registry.getUser("joe")).willReturn(simpUser);
this.resolver = new DefaultUserDestinationResolver(this.registry);
}
@Test

View File

@@ -23,7 +23,6 @@ import java.util.Iterator;
import java.util.Set;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.messaging.Message;
import org.springframework.messaging.converter.MappingJackson2MessageConverter;
@@ -40,7 +39,7 @@ import static org.mockito.Mockito.mock;
*/
class MultiServerUserRegistryTests {
private final SimpUserRegistry localRegistry = Mockito.mock(SimpUserRegistry.class);
private final SimpUserRegistry localRegistry = mock();
private final MultiServerUserRegistry registry = new MultiServerUserRegistry(this.localRegistry);
@@ -49,7 +48,7 @@ class MultiServerUserRegistryTests {
@Test
void getUserFromLocalRegistry() {
SimpUser user = Mockito.mock(SimpUser.class);
SimpUser user = mock();
Set<SimpUser> users = Collections.singleton(user);
given(this.localRegistry.getUsers()).willReturn(users);
given(this.localRegistry.getUserCount()).willReturn(1);
@@ -66,7 +65,7 @@ class MultiServerUserRegistryTests {
TestSimpSession testSession = new TestSimpSession("remote-sess");
testSession.addSubscriptions(new TestSimpSubscription("remote-sub", "/remote-dest"));
testUser.addSessions(testSession);
SimpUserRegistry testRegistry = mock(SimpUserRegistry.class);
SimpUserRegistry testRegistry = mock();
given(testRegistry.getUsers()).willReturn(Collections.singleton(testUser));
Object registryDto = new MultiServerUserRegistry(testRegistry).getLocalRegistryDto();
Message<?> message = this.converter.toMessage(registryDto, null);
@@ -105,7 +104,7 @@ class MultiServerUserRegistryTests {
user1.addSessions(session1);
user2.addSessions(session2);
user3.addSessions(session3);
SimpUserRegistry userRegistry = mock(SimpUserRegistry.class);
SimpUserRegistry userRegistry = mock();
given(userRegistry.getUsers()).willReturn(new HashSet<>(Arrays.asList(user1, user2, user3)));
Object registryDto = new MultiServerUserRegistry(userRegistry).getLocalRegistryDto();
Message<?> message = this.converter.toMessage(registryDto, null);
@@ -135,7 +134,7 @@ class MultiServerUserRegistryTests {
TestSimpUser remoteUser = new TestSimpUser("joe");
TestSimpSession remoteSession = new TestSimpSession("sess456");
remoteUser.addSessions(remoteSession);
SimpUserRegistry remoteRegistry = mock(SimpUserRegistry.class);
SimpUserRegistry remoteRegistry = mock();
given(remoteRegistry.getUsers()).willReturn(Collections.singleton(remoteUser));
Object remoteRegistryDto = new MultiServerUserRegistry(remoteRegistry).getLocalRegistryDto();
Message<?> message = this.converter.toMessage(remoteRegistryDto, null);
@@ -164,7 +163,7 @@ class MultiServerUserRegistryTests {
// Prepare broadcast message from remote server
TestSimpUser testUser = new TestSimpUser("joe");
testUser.addSessions(new TestSimpSession("remote-sub"));
SimpUserRegistry testRegistry = mock(SimpUserRegistry.class);
SimpUserRegistry testRegistry = mock();
given(testRegistry.getUsers()).willReturn(Collections.singleton(testUser));
Object registryDto = new MultiServerUserRegistry(testRegistry).getLocalRegistryDto();
Message<?> message = this.converter.toMessage(registryDto, null);

View File

@@ -46,9 +46,9 @@ class UserDestinationMessageHandlerTests {
private static final String SESSION_ID = "123";
private final SimpUserRegistry registry = mock(SimpUserRegistry.class);
private final SimpUserRegistry registry = mock();
private final SubscribableChannel brokerChannel = mock(SubscribableChannel.class);
private final SubscribableChannel brokerChannel = mock();
private final UserDestinationMessageHandler handler = new UserDestinationMessageHandler(new StubMessageChannel(), this.brokerChannel, new DefaultUserDestinationResolver(this.registry));

View File

@@ -24,10 +24,7 @@ import java.util.concurrent.ScheduledFuture;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.messaging.Message;
import org.springframework.messaging.MessageChannel;
@@ -49,52 +46,43 @@ import static org.mockito.Mockito.verifyNoMoreInteractions;
/**
* User tests for {@link UserRegistryMessageHandler}.
*
* @author Rossen Stoyanchev
*/
@ExtendWith(MockitoExtension.class)
public class UserRegistryMessageHandlerTests {
class UserRegistryMessageHandlerTests {
private SimpUserRegistry localRegistry = mock();
private MessageChannel brokerChannel = mock();
private TaskScheduler taskScheduler = mock();
private MultiServerUserRegistry multiServerRegistry = new MultiServerUserRegistry(this.localRegistry);
private MessageConverter converter = new MappingJackson2MessageConverter();
private UserRegistryMessageHandler handler;
private SimpUserRegistry localRegistry;
private MultiServerUserRegistry multiServerRegistry;
private MessageConverter converter;
@Mock
private MessageChannel brokerChannel;
@Mock
private TaskScheduler taskScheduler;
@BeforeEach
public void setUp() throws Exception {
this.converter = new MappingJackson2MessageConverter();
void setUp() throws Exception {
SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.brokerChannel);
brokerTemplate.setMessageConverter(this.converter);
this.localRegistry = mock(SimpUserRegistry.class);
this.multiServerRegistry = new MultiServerUserRegistry(this.localRegistry);
this.handler = new UserRegistryMessageHandler(this.multiServerRegistry, brokerTemplate,
"/topic/simp-user-registry", this.taskScheduler);
}
@Test
public void brokerAvailableEvent() throws Exception {
void brokerAvailableEvent() throws Exception {
Runnable runnable = getUserRegistryTask();
assertThat(runnable).isNotNull();
}
@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void brokerUnavailableEvent() throws Exception {
ScheduledFuture future = mock(ScheduledFuture.class);
void brokerUnavailableEvent() throws Exception {
ScheduledFuture future = mock();
given(this.taskScheduler.scheduleWithFixedDelay(any(Runnable.class), any(Duration.class))).willReturn(future);
BrokerAvailabilityEvent event = new BrokerAvailabilityEvent(true, this);
@@ -108,7 +96,7 @@ public class UserRegistryMessageHandlerTests {
@Test
@SuppressWarnings("rawtypes")
public void broadcastRegistry() throws Exception {
void broadcastRegistry() throws Exception {
given(this.brokerChannel.send(any())).willReturn(true);
TestSimpUser simpUser1 = new TestSimpUser("joe");
@@ -130,7 +118,7 @@ public class UserRegistryMessageHandlerTests {
MessageHeaders headers = message.getHeaders();
assertThat(SimpMessageHeaderAccessor.getDestination(headers)).isEqualTo("/topic/simp-user-registry");
MultiServerUserRegistry remoteRegistry = new MultiServerUserRegistry(mock(SimpUserRegistry.class));
MultiServerUserRegistry remoteRegistry = new MultiServerUserRegistry(mock());
remoteRegistry.addRemoteRegistryDto(message, this.converter, 20000);
assertThat(remoteRegistry.getUserCount()).isEqualTo(2);
assertThat(remoteRegistry.getUser("joe")).isNotNull();
@@ -138,8 +126,7 @@ public class UserRegistryMessageHandlerTests {
}
@Test
public void handleMessage() throws Exception {
void handleMessage() throws Exception {
TestSimpUser simpUser1 = new TestSimpUser("joe");
TestSimpUser simpUser2 = new TestSimpUser("jane");
@@ -147,7 +134,7 @@ public class UserRegistryMessageHandlerTests {
simpUser2.addSessions(new TestSimpSession("456"));
HashSet<SimpUser> simpUsers = new HashSet<>(Arrays.asList(simpUser1, simpUser2));
SimpUserRegistry remoteUserRegistry = mock(SimpUserRegistry.class);
SimpUserRegistry remoteUserRegistry = mock();
given(remoteUserRegistry.getUserCount()).willReturn(2);
given(remoteUserRegistry.getUsers()).willReturn(simpUsers);
@@ -162,8 +149,7 @@ public class UserRegistryMessageHandlerTests {
}
@Test
public void handleMessageFromOwnBroadcast() throws Exception {
void handleMessageFromOwnBroadcast() throws Exception {
TestSimpUser simpUser = new TestSimpUser("joe");
simpUser.addSessions(new TestSimpSession("123"));
given(this.localRegistry.getUserCount()).willReturn(1);

View File

@@ -54,7 +54,7 @@ public class ChannelInterceptorTests {
@Test
public void preSendInterceptorReturningModifiedMessage() {
Message<?> expected = mock(Message.class);
Message<?> expected = mock();
PreSendInterceptor interceptor = new PreSendInterceptor();
interceptor.setMessageToReturn(expected);
this.channel.addInterceptor(interceptor);

View File

@@ -83,7 +83,7 @@ public class ExecutorSubscribableChannelTests {
@Test
public void sendWithExecutor() {
BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor();
TaskExecutor executor = mock(TaskExecutor.class);
TaskExecutor executor = mock();
ExecutorSubscribableChannel testChannel = new ExecutorSubscribableChannel(executor);
testChannel.addInterceptor(interceptor);
testChannel.subscribe(this.handler);
@@ -117,7 +117,7 @@ public class ExecutorSubscribableChannelTests {
public void failurePropagates() {
RuntimeException ex = new RuntimeException();
willThrow(ex).given(this.handler).handleMessage(this.message);
MessageHandler secondHandler = mock(MessageHandler.class);
MessageHandler secondHandler = mock();
this.channel.subscribe(this.handler);
this.channel.subscribe(secondHandler);
try {
@@ -139,7 +139,7 @@ public class ExecutorSubscribableChannelTests {
@Test
public void interceptorWithModifiedMessage() {
Message<?> expected = mock(Message.class);
Message<?> expected = mock();
BeforeHandleInterceptor interceptor = new BeforeHandleInterceptor();
interceptor.setMessageToReturn(expected);
this.channel.addInterceptor(interceptor);