Skip to content

Commit 141ef90

Browse files
committed
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.
1 parent d495902 commit 141ef90

File tree

27 files changed

+170
-290
lines changed

27 files changed

+170
-290
lines changed

spring-jdbc/src/test/java/org/springframework/jdbc/core/support/SqlLobValueTests.java

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,11 @@
2525

2626
import org.junit.Before;
2727
import org.junit.Test;
28+
import org.junit.runner.RunWith;
2829
import org.mockito.ArgumentCaptor;
2930
import org.mockito.Captor;
30-
import org.mockito.MockitoAnnotations;
31+
import org.mockito.Mock;
32+
import org.mockito.junit.MockitoJUnitRunner;
3133

3234
import org.springframework.jdbc.support.lob.LobCreator;
3335
import org.springframework.jdbc.support.lob.LobHandler;
@@ -36,11 +38,10 @@
3638
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
3739
import static org.mockito.ArgumentMatchers.eq;
3840
import static org.mockito.BDDMockito.given;
39-
import static org.mockito.Mockito.mock;
4041
import static org.mockito.Mockito.verify;
4142

4243
/**
43-
* Test cases for the sql lob value:
44+
* Test cases for the SQL LOB value:
4445
*
4546
* BLOB:
4647
* 1. Types.BLOB: setBlobAsBytes (byte[])
@@ -55,21 +56,23 @@
5556
*
5657
* @author Alef Arendsen
5758
*/
59+
@RunWith(MockitoJUnitRunner.class)
5860
public class SqlLobValueTests {
5961

62+
@Mock
6063
private PreparedStatement preparedStatement;
64+
65+
@Mock
6166
private LobHandler handler;
67+
68+
@Mock
6269
private LobCreator creator;
6370

6471
@Captor
6572
private ArgumentCaptor<InputStream> inputStreamCaptor;
6673

6774
@Before
6875
public void setUp() {
69-
MockitoAnnotations.initMocks(this);
70-
preparedStatement = mock(PreparedStatement.class);
71-
handler = mock(LobHandler.class);
72-
creator = mock(LobCreator.class);
7376
given(handler.getLobCreator()).willReturn(creator);
7477
}
7578

spring-jms/src/test/java/org/springframework/jms/core/JmsMessagingTemplateTests.java

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828

2929
import org.junit.Before;
3030
import org.junit.Test;
31+
import org.junit.runner.RunWith;
3132
import org.mockito.ArgumentCaptor;
32-
import org.mockito.BDDMockito;
3333
import org.mockito.Captor;
3434
import org.mockito.Mock;
35-
import org.mockito.MockitoAnnotations;
35+
import org.mockito.junit.MockitoJUnitRunner;
3636
import org.mockito.stubbing.Answer;
3737

3838
import org.springframework.beans.DirectFieldAccessor;
@@ -65,6 +65,7 @@
6565
*
6666
* @author Stephane Nicoll
6767
*/
68+
@RunWith(MockitoJUnitRunner.class)
6869
public class JmsMessagingTemplateTests {
6970

7071
@Captor
@@ -78,7 +79,6 @@ public class JmsMessagingTemplateTests {
7879

7980
@Before
8081
public void setup() {
81-
MockitoAnnotations.initMocks(this);
8282
this.messagingTemplate = new JmsMessagingTemplate(this.jmsTemplate);
8383
}
8484

@@ -108,8 +108,6 @@ public void payloadConverterIsConsistentSetter() {
108108

109109
@Test
110110
public void customConverterAlwaysTakesPrecedence() {
111-
MessageConverter messageConverter = mock(MessageConverter.class);
112-
given(this.jmsTemplate.getMessageConverter()).willReturn(messageConverter);
113111
MessageConverter customMessageConverter = mock(MessageConverter.class);
114112
JmsMessagingTemplate messagingTemplate = new JmsMessagingTemplate();
115113
messagingTemplate.setJmsMessageConverter(
@@ -648,11 +646,11 @@ private void assertTextMessage(Message<?> message) {
648646

649647
protected TextMessage createTextMessage(MessageCreator creator) throws JMSException {
650648
Session mock = mock(Session.class);
651-
given(mock.createTextMessage(BDDMockito.any())).willAnswer(
649+
given(mock.createTextMessage(any())).willAnswer(
652650
(Answer<TextMessage>) invocation ->
653651
new StubTextMessage((String) invocation.getArguments()[0]));
654652
javax.jms.Message message = creator.createMessage(mock);
655-
verify(mock).createTextMessage(BDDMockito.any());
653+
verify(mock).createTextMessage(any());
656654
return TextMessage.class.cast(message);
657655
}
658656

spring-messaging/src/test/java/org/springframework/messaging/simp/SimpAttributesTests.java

Lines changed: 11 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -19,34 +19,25 @@
1919
import java.util.Map;
2020
import java.util.concurrent.ConcurrentHashMap;
2121

22-
import org.junit.Before;
2322
import org.junit.Test;
24-
import org.mockito.Mockito;
2523

2624
import static org.assertj.core.api.Assertions.assertThat;
2725
import static org.assertj.core.api.Assertions.assertThatIllegalStateException;
26+
import static org.mockito.Mockito.mock;
2827
import static org.mockito.Mockito.times;
2928
import static org.mockito.Mockito.verify;
3029

3130
/**
32-
* Unit tests for
33-
* {@link org.springframework.messaging.simp.SimpAttributes}.
31+
* Unit tests for {@link SimpAttributes}.
3432
*
3533
* @author Rossen Stoyanchev
3634
* @since 4.1
3735
*/
3836
public class SimpAttributesTests {
3937

40-
private SimpAttributes simpAttributes;
38+
private final Map<String, Object> map = new ConcurrentHashMap<>();
4139

42-
private Map<String, Object> map;
43-
44-
45-
@Before
46-
public void setup() {
47-
this.map = new ConcurrentHashMap<>();
48-
this.simpAttributes = new SimpAttributes("session1", this.map);
49-
}
40+
private final SimpAttributes simpAttributes = new SimpAttributes("session1", this.map);
5041

5142

5243
@Test
@@ -69,7 +60,7 @@ public void getAttributeNames() {
6960

7061
@Test
7162
public void registerDestructionCallback() {
72-
Runnable callback = Mockito.mock(Runnable.class);
63+
Runnable callback = mock(Runnable.class);
7364
this.simpAttributes.registerDestructionCallback("name1", callback);
7465

7566
assertThat(this.simpAttributes.getAttribute(
@@ -80,14 +71,14 @@ public void registerDestructionCallback() {
8071
public void registerDestructionCallbackAfterSessionCompleted() {
8172
this.simpAttributes.sessionCompleted();
8273
assertThatIllegalStateException().isThrownBy(() ->
83-
this.simpAttributes.registerDestructionCallback("name1", Mockito.mock(Runnable.class)))
74+
this.simpAttributes.registerDestructionCallback("name1", mock(Runnable.class)))
8475
.withMessageContaining("already completed");
8576
}
8677

8778
@Test
8879
public void removeDestructionCallback() {
89-
Runnable callback1 = Mockito.mock(Runnable.class);
90-
Runnable callback2 = Mockito.mock(Runnable.class);
80+
Runnable callback1 = mock(Runnable.class);
81+
Runnable callback2 = mock(Runnable.class);
9182
this.simpAttributes.registerDestructionCallback("name1", callback1);
9283
this.simpAttributes.registerDestructionCallback("name2", callback2);
9384

@@ -109,8 +100,8 @@ public void getSessionMutexExplicit() {
109100

110101
@Test
111102
public void sessionCompleted() {
112-
Runnable callback1 = Mockito.mock(Runnable.class);
113-
Runnable callback2 = Mockito.mock(Runnable.class);
103+
Runnable callback1 = mock(Runnable.class);
104+
Runnable callback2 = mock(Runnable.class);
114105
this.simpAttributes.registerDestructionCallback("name1", callback1);
115106
this.simpAttributes.registerDestructionCallback("name2", callback2);
116107

@@ -122,7 +113,7 @@ public void sessionCompleted() {
122113

123114
@Test
124115
public void sessionCompletedIsIdempotent() {
125-
Runnable callback1 = Mockito.mock(Runnable.class);
116+
Runnable callback1 = mock(Runnable.class);
126117
this.simpAttributes.registerDestructionCallback("name1", callback1);
127118

128119
this.simpAttributes.sessionCompleted();

spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SendToMethodReturnValueHandlerTests.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,11 @@
2828
import com.fasterxml.jackson.annotation.JsonView;
2929
import org.junit.Before;
3030
import org.junit.Test;
31+
import org.junit.runner.RunWith;
3132
import org.mockito.ArgumentCaptor;
3233
import org.mockito.Captor;
3334
import org.mockito.Mock;
34-
import org.mockito.Mockito;
35-
import org.mockito.MockitoAnnotations;
35+
import org.mockito.junit.MockitoJUnitRunner;
3636

3737
import org.springframework.core.MethodParameter;
3838
import org.springframework.core.annotation.AliasFor;
@@ -58,6 +58,7 @@
5858
import static org.mockito.ArgumentMatchers.any;
5959
import static org.mockito.ArgumentMatchers.eq;
6060
import static org.mockito.BDDMockito.given;
61+
import static org.mockito.Mockito.mock;
6162
import static org.mockito.Mockito.times;
6263
import static org.mockito.Mockito.verify;
6364

@@ -68,23 +69,26 @@
6869
* @author Sebastien Deleuze
6970
* @author Stephane Nicoll
7071
*/
72+
@RunWith(MockitoJUnitRunner.class)
7173
public class SendToMethodReturnValueHandlerTests {
7274

7375
private static final MimeType MIME_TYPE = new MimeType("text", "plain", StandardCharsets.UTF_8);
7476

7577
private static final String PAYLOAD = "payload";
7678

7779

80+
@Mock
81+
private MessageChannel messageChannel;
82+
83+
@Captor
84+
private ArgumentCaptor<Message<?>> messageCaptor;
85+
7886
private SendToMethodReturnValueHandler handler;
7987

8088
private SendToMethodReturnValueHandler handlerAnnotationNotRequired;
8189

8290
private SendToMethodReturnValueHandler jsonHandler;
8391

84-
@Mock private MessageChannel messageChannel;
85-
86-
@Captor private ArgumentCaptor<Message<?>> messageCaptor;
87-
8892
private MethodParameter noAnnotationsReturnType = param("handleNoAnnotations");
8993
private MethodParameter sendToReturnType = param("handleAndSendTo");
9094
private MethodParameter sendToDefaultDestReturnType = param("handleAndSendToDefaultDest");
@@ -118,8 +122,6 @@ private static MethodParameter param(Class<?> clazz, String methodName) {
118122

119123
@Before
120124
public void setup() throws Exception {
121-
MockitoAnnotations.initMocks(this);
122-
123125
SimpMessagingTemplate messagingTemplate = new SimpMessagingTemplate(this.messageChannel);
124126
messagingTemplate.setMessageConverter(new StringMessageConverter());
125127
this.handler = new SendToMethodReturnValueHandler(messagingTemplate, true);
@@ -319,7 +321,7 @@ public void sendToDefaultDestinationWhenUsingDotPathSeparator() throws Exception
319321
public void testHeadersToSend() throws Exception {
320322
Message<?> message = createMessage("sess1", "sub1", "/app", "/dest", null);
321323

322-
SimpMessageSendingOperations messagingTemplate = Mockito.mock(SimpMessageSendingOperations.class);
324+
SimpMessageSendingOperations messagingTemplate = mock(SimpMessageSendingOperations.class);
323325
SendToMethodReturnValueHandler handler = new SendToMethodReturnValueHandler(messagingTemplate, false);
324326

325327
handler.handleReturnValue(PAYLOAD, this.noAnnotationsReturnType, message);
@@ -630,10 +632,12 @@ JacksonViewBean handleAndSendToJsonView() {
630632

631633
private static class TestUser implements Principal {
632634

635+
@Override
633636
public String getName() {
634637
return "joe";
635638
}
636639

640+
@Override
637641
public boolean implies(Subject subject) {
638642
return false;
639643
}

spring-messaging/src/test/java/org/springframework/messaging/simp/annotation/support/SimpAnnotationMethodMessageHandlerTests.java

Lines changed: 8 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@
2727

2828
import org.junit.Before;
2929
import org.junit.Test;
30+
import org.junit.runner.RunWith;
3031
import org.mockito.ArgumentCaptor;
3132
import org.mockito.Captor;
3233
import org.mockito.Mock;
33-
import org.mockito.MockitoAnnotations;
34+
import org.mockito.junit.MockitoJUnitRunner;
3435
import reactor.core.publisher.EmitterProcessor;
3536
import reactor.core.publisher.Flux;
3637
import reactor.core.publisher.FluxProcessor;
@@ -74,22 +75,18 @@
7475
import static org.mockito.Mockito.verify;
7576

7677
/**
77-
* Test fixture for
78-
* {@link org.springframework.messaging.simp.annotation.support.SimpAnnotationMethodMessageHandler}.
78+
* Test fixture for {@link SimpAnnotationMethodMessageHandler}.
7979
*
8080
* @author Rossen Stoyanchev
8181
* @author Brian Clozel
8282
* @author Sebastien Deleuze
8383
*/
84+
@RunWith(MockitoJUnitRunner.class)
8485
public class SimpAnnotationMethodMessageHandlerTests {
8586

8687
private static final String TEST_INVALID_VALUE = "invalidValue";
8788

8889

89-
private TestSimpAnnotationMethodMessageHandler messageHandler;
90-
91-
private TestController testController;
92-
9390
@Mock
9491
private SubscribableChannel channel;
9592

@@ -99,20 +96,20 @@ public class SimpAnnotationMethodMessageHandlerTests {
9996
@Captor
10097
private ArgumentCaptor<Object> payloadCaptor;
10198

99+
private TestSimpAnnotationMethodMessageHandler messageHandler;
100+
101+
private TestController testController = new TestController();
102+
102103

103104
@Before
104105
public void setup() {
105-
MockitoAnnotations.initMocks(this);
106-
107106
SimpMessagingTemplate brokerTemplate = new SimpMessagingTemplate(this.channel);
108107
brokerTemplate.setMessageConverter(this.converter);
109108

110109
this.messageHandler = new TestSimpAnnotationMethodMessageHandler(brokerTemplate, this.channel, this.channel);
111110
this.messageHandler.setApplicationContext(new StaticApplicationContext());
112111
this.messageHandler.setValidator(new StringTestValidator(TEST_INVALID_VALUE));
113112
this.messageHandler.afterPropertiesSet();
114-
115-
this.testController = new TestController();
116113
}
117114

118115

@@ -287,12 +284,7 @@ public void listenableFutureSuccess() {
287284
}
288285

289286
@Test
290-
@SuppressWarnings({ "unchecked", "rawtypes" })
291287
public void listenableFutureFailure() {
292-
Message emptyMessage = MessageBuilder.withPayload(new byte[0]).build();
293-
given(this.channel.send(any(Message.class))).willReturn(true);
294-
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
295-
296288
ListenableFutureController controller = new ListenableFutureController();
297289
this.messageHandler.registerHandler(controller);
298290
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
@@ -325,12 +317,7 @@ public void completableFutureSuccess() {
325317
}
326318

327319
@Test
328-
@SuppressWarnings({ "unchecked", "rawtypes" })
329320
public void completableFutureFailure() {
330-
Message emptyMessage = MessageBuilder.withPayload(new byte[0]).build();
331-
given(this.channel.send(any(Message.class))).willReturn(true);
332-
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
333-
334321
CompletableFutureController controller = new CompletableFutureController();
335322
this.messageHandler.registerHandler(controller);
336323
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
@@ -363,12 +350,7 @@ public void monoSuccess() {
363350
}
364351

365352
@Test
366-
@SuppressWarnings({ "unchecked", "rawtypes" })
367353
public void monoFailure() {
368-
Message emptyMessage = MessageBuilder.withPayload(new byte[0]).build();
369-
given(this.channel.send(any(Message.class))).willReturn(true);
370-
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
371-
372354
ReactiveController controller = new ReactiveController();
373355
this.messageHandler.registerHandler(controller);
374356
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));
@@ -381,12 +363,7 @@ public void monoFailure() {
381363
}
382364

383365
@Test
384-
@SuppressWarnings({ "unchecked", "rawtypes" })
385366
public void fluxNotHandled() {
386-
Message emptyMessage = MessageBuilder.withPayload(new byte[0]).build();
387-
given(this.channel.send(any(Message.class))).willReturn(true);
388-
given(this.converter.toMessage(any(), any(MessageHeaders.class))).willReturn(emptyMessage);
389-
390367
ReactiveController controller = new ReactiveController();
391368
this.messageHandler.registerHandler(controller);
392369
this.messageHandler.setDestinationPrefixes(Arrays.asList("/app1", "/app2/"));

0 commit comments

Comments
 (0)