Skip to content

Commit 622bdb6

Browse files
committed
Move Tests to Mockito4
1 parent c6cf180 commit 622bdb6

File tree

45 files changed

+142
-136
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+142
-136
lines changed

OCPP-J/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@
7272
<dependency>
7373
<groupId>org.mockito</groupId>
7474
<artifactId>mockito-core</artifactId>
75-
<version>1.10.19</version>
75+
<version>4.11.0</version>
7676
<scope>test</scope>
7777
</dependency>
7878
<dependency>

OCPP-J/src/test/java/eu/chargetime/ocpp/wss/test/BaseWssSocketBuilderTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import static org.hamcrest.CoreMatchers.is;
44
import static org.junit.Assert.*;
5-
import static org.mockito.Matchers.any;
5+
import static org.mockito.ArgumentMatchers.any;
66
import static org.mockito.Mockito.*;
77

88
import eu.chargetime.ocpp.wss.BaseWssFactoryBuilder;

ocpp-common/pom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@
8080
<dependency>
8181
<groupId>org.mockito</groupId>
8282
<artifactId>mockito-core</artifactId>
83-
<version>1.10.19</version>
83+
<version>4.11.0</version>
8484
<scope>test</scope>
8585
</dependency>
8686
<dependency>

ocpp-common/src/test/java/eu/chargetime/ocpp/test/ClientTest.java

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ of this software and associated documentation files (the "Software"), to deal
4141
import org.junit.Test;
4242
import org.junit.runner.RunWith;
4343
import org.mockito.Mock;
44-
import org.mockito.runners.MockitoJUnitRunner;
44+
import org.mockito.junit.MockitoJUnitRunner;
4545

4646
@RunWith(MockitoJUnitRunner.class)
4747
public class ClientTest {
@@ -59,7 +59,7 @@ public class ClientTest {
5959
@Before
6060
public void setup() {
6161
when(request.validate()).thenReturn(true);
62-
doAnswer(invocation -> eventHandler = invocation.getArgumentAt(1, SessionEvents.class))
62+
doAnswer(invocation -> eventHandler = invocation.getArgument(1, SessionEvents.class))
6363
.when(session)
6464
.open(any(), any());
6565

@@ -77,7 +77,7 @@ public void connect_connects() {
7777
client.connect(someUrl, events);
7878

7979
// Then
80-
verify(session, times(1)).open(eq(someUrl), anyObject());
80+
verify(session).open(eq(someUrl), any());
8181
}
8282

8383
@Test
@@ -89,7 +89,7 @@ public void connect_connectionOpenedEvent() {
8989
this.eventHandler.handleConnectionOpened();
9090

9191
// Then
92-
verify(events, times(1)).connectionOpened();
92+
verify(events).connectionOpened();
9393
verify(events, never()).connectionClosed();
9494
}
9595

@@ -102,7 +102,7 @@ public void connect_connectionClosedEvent() {
102102
this.eventHandler.handleConnectionClosed();
103103

104104
// Then
105-
verify(events, times(1)).connectionClosed();
105+
verify(events).connectionClosed();
106106
verify(events, never()).connectionOpened();
107107
}
108108

@@ -112,14 +112,15 @@ public void send_aMessage_isCommunicated() throws Exception {
112112
client.send(request);
113113

114114
// Then
115-
verify(session, times(1)).sendRequest(anyString(), eq(request), anyString());
115+
// TODO action and uuid should not be nullable
116+
verify(session).sendRequest(nullable(String.class), eq(request), nullable(String.class));
116117
}
117118

118119
@Test
119120
public void responseReceived_aMessageWasSend_PromiseIsCompleted() throws Exception {
120121
// Given
121122
String someUniqueId = "Some id";
122-
when(session.storeRequest(any())).thenReturn(someUniqueId);
123+
lenient().when(session.storeRequest(any())).thenReturn(someUniqueId);
123124
when(promiseRepository.getPromise(any())).thenReturn(Optional.of(completableFuture));
124125

125126
// When
@@ -152,7 +153,7 @@ public void handleRequest_callsFeatureHandleRequest() throws UnsupportedFeatureE
152153
eventHandler.handleRequest(request);
153154

154155
// Then
155-
verify(feature, times(1)).handleRequest(any(), eq(request));
156+
verify(feature).handleRequest(any(), eq(request));
156157
}
157158

158159
@Test
@@ -161,6 +162,6 @@ public void send_aMessage_validatesMessage() throws Exception {
161162
client.send(request);
162163

163164
// Then
164-
verify(request, times(1)).validate();
165+
verify(request).validate();
165166
}
166167
}

ocpp-common/src/test/java/eu/chargetime/ocpp/test/CommunicatorTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package eu.chargetime.ocpp.test;
22

3-
import static org.mockito.Matchers.anyString;
3+
import static org.mockito.ArgumentMatchers.anyString;
44
import static org.mockito.Mockito.*;
55

66
import eu.chargetime.ocpp.*;
@@ -12,7 +12,7 @@
1212
import org.junit.Test;
1313
import org.junit.runner.RunWith;
1414
import org.mockito.Mock;
15-
import org.mockito.runners.MockitoJUnitRunner;
15+
import org.mockito.junit.MockitoJUnitRunner;
1616

1717
/*
1818
ChargeTime.eu - Java-OCA-OCPP
@@ -57,7 +57,7 @@ public void setup() throws Exception {
5757

5858
when(transactionRelatedRequest.transactionRelated()).thenReturn(true);
5959
when(normalRequest.transactionRelated()).thenReturn(false);
60-
doAnswer(invocation -> eventHandler = invocation.getArgumentAt(0, RadioEvents.class))
60+
doAnswer(invocation -> eventHandler = invocation.getArgument(0, RadioEvents.class))
6161
.when(receiver)
6262
.accept(any());
6363
setupCommunicator(true);
@@ -126,7 +126,7 @@ public void sendCall_aNormalRequestAndRadioThrowsNotConnectedException_onErrorIs
126126
communicator.sendCall(uniqueId, action, normalRequest);
127127

128128
// Then
129-
verify(events, times(1)).onError(eq(uniqueId), any(), any(), any());
129+
verify(events).onError(eq(uniqueId), any(), any(), any());
130130
}
131131

132132
@Test
@@ -164,7 +164,7 @@ public void sendCall_queueNotEmpty_messagesAreProcessInSequence() throws Excepti
164164

165165
// Then
166166
verify(receiver, times(2)).send(eq(firstId));
167-
verify(receiver, times(1)).send(eq(secondId));
167+
verify(receiver).send(eq(secondId));
168168
}
169169

170170
@Test
@@ -222,7 +222,7 @@ public boolean validate() {
222222
communicator.sendCallResult(uniqueId, action, conf);
223223

224224
// Then
225-
verify(handler, times(1)).onConfirmationCompleted();
225+
verify(handler).onConfirmationCompleted();
226226
}
227227

228228
@Test

ocpp-common/src/test/java/eu/chargetime/ocpp/test/ServerTest.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
import org.junit.Test;
1313
import org.junit.runner.RunWith;
1414
import org.mockito.Mock;
15-
import org.mockito.runners.MockitoJUnitRunner;
15+
import org.mockito.junit.MockitoJUnitRunner;
1616

1717
/*
1818
ChargeTime.eu - Java-OCA-OCPP
@@ -65,13 +65,14 @@ public void setup() {
6565
UUID sessionId = UUID.randomUUID();
6666
when(request.validate()).thenReturn(true);
6767
when(session.getSessionId()).thenReturn(sessionId);
68-
doAnswer(invocation -> listenerEvents = invocation.getArgumentAt(2, ListenerEvents.class))
68+
// when(session.storeRequest(any())).thenReturn(UUID.randomUUID().toString());
69+
doAnswer(invocation -> listenerEvents = invocation.getArgument(2, ListenerEvents.class))
6970
.when(listener)
7071
.open(anyString(), anyInt(), any());
71-
doAnswer(invocation -> sessionEvents = invocation.getArgumentAt(0, SessionEvents.class))
72+
doAnswer(invocation -> sessionEvents = invocation.getArgument(0, SessionEvents.class))
7273
.when(session)
7374
.accept(any());
74-
doAnswer(invocation -> sessionIndex = invocation.getArgumentAt(0, UUID.class))
75+
doAnswer(invocation -> sessionIndex = invocation.getArgument(0, UUID.class))
7576
.when(serverEvents)
7677
.newSession(any(), any());
7778

@@ -89,7 +90,7 @@ public void newSession_serverIsListening_sessionIsAccepted() {
8990
listenerEvents.newSession(session, information);
9091

9192
// Then
92-
verify(session, times(1)).accept(any());
93+
verify(session).accept(any());
9394
}
9495

9596
@Test
@@ -101,7 +102,7 @@ public void newSession_serverIsListening_callbackWithIndex0() {
101102
listenerEvents.newSession(session, information);
102103

103104
// Then
104-
verify(serverEvents, times(1)).newSession(any(UUID.class), eq(information));
105+
verify(serverEvents).newSession(any(UUID.class), eq(information));
105106
}
106107

107108
@Test
@@ -114,7 +115,8 @@ public void send_aMessage_isCommunicated() throws Exception {
114115
server.send(sessionIndex, request);
115116

116117
// Then
117-
verify(session, times(1)).sendRequest(anyString(), eq(request), anyString());
118+
// TODO action and uuid should not be nullable
119+
verify(session).sendRequest(nullable(String.class), eq(request), nullable(String.class));
118120
}
119121

120122
@Test
@@ -127,7 +129,7 @@ public void handleRequest_callsFeatureHandleRequest() throws UnsupportedFeatureE
127129
sessionEvents.handleRequest(request);
128130

129131
// Then
130-
verify(feature, times(1)).handleRequest(any(UUID.class), eq(request));
132+
verify(feature).handleRequest(any(UUID.class), eq(request));
131133
}
132134

133135
@Test
@@ -140,6 +142,6 @@ public void send_aMessage_validatesMessage() throws Exception {
140142
server.send(sessionIndex, request);
141143

142144
// Then
143-
verify(request, times(1)).validate();
145+
verify(request).validate();
144146
}
145147
}

ocpp-common/src/test/java/eu/chargetime/ocpp/test/SessionTest.java

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
import org.junit.Test;
1717
import org.junit.runner.RunWith;
1818
import org.mockito.Mock;
19-
import org.mockito.runners.MockitoJUnitRunner;
19+
import org.mockito.junit.MockitoJUnitRunner;
2020

2121
/*
2222
ChargeTime.eu - Java-OCA-OCPP
@@ -63,7 +63,7 @@ public class SessionTest {
6363
public void setup() throws Exception {
6464
when(featureRepository.findFeature(any())).thenReturn(Optional.of(feature));
6565
session = new Session(communicator, queue, fulfiller, featureRepository);
66-
doAnswer(invocation -> eventHandler = invocation.getArgumentAt(1, CommunicatorEvents.class))
66+
doAnswer(invocation -> eventHandler = invocation.getArgument(1, CommunicatorEvents.class))
6767
.when(communicator)
6868
.connect(any(), any());
6969
session.open(null, sessionEvents);
@@ -103,7 +103,8 @@ public boolean validate() {
103103
session.sendRequest(someAction, someRequest, null);
104104

105105
// Then
106-
verify(communicator, times(1)).sendCall(anyString(), eq(someAction), eq(someRequest));
106+
// TODO uniqueid should not be nullable
107+
verify(communicator).sendCall(nullable(String.class), eq(someAction), eq(someRequest));
107108
}
108109

109110
@Test
@@ -115,7 +116,8 @@ public void sendRequest_someUniqueId_sendsUniqueIdToCommunicator() {
115116
session.sendRequest(null, null, someUniqueId);
116117

117118
// Then
118-
verify(communicator, times(1)).sendCall(eq(someUniqueId), anyString(), any());
119+
// TODO uuid and request should not be nullable
120+
verify(communicator).sendCall(eq(someUniqueId), nullable(String.class), nullable(Request.class));
119121
}
120122

121123
@Test
@@ -134,7 +136,7 @@ public boolean validate() {
134136
session.sendConfirmation(someUniqueId, action, conf);
135137

136138
// Then
137-
verify(communicator, times(1)).sendCallResult(eq(someUniqueId), eq(action), eq(conf));
139+
verify(communicator).sendCallResult(eq(someUniqueId), eq(action), eq(conf));
138140
}
139141

140142
@Test
@@ -146,14 +148,14 @@ public void open_connectsViaCommunicator() {
146148
session.open(someUri, null);
147149

148150
// Then
149-
verify(communicator, times(1)).connect(eq(someUri), any());
151+
verify(communicator).connect(eq(someUri), any());
150152
}
151153

152154
@Test
153155
public void onCall_unhandledCallback_callSendCallError() throws Exception {
154156
// Given
155157
String someId = "Some id";
156-
doAnswer(invocation -> invocation.getArgumentAt(0, CompletableFuture.class).complete(null))
158+
doAnswer(invocation -> invocation.getArgument(0, CompletableFuture.class).complete(null))
157159
.when(fulfiller)
158160
.fulfill(any(), any(), any());
159161
when(communicator.unpackPayload(any(), any())).thenReturn(new TestRequest());
@@ -162,7 +164,8 @@ public void onCall_unhandledCallback_callSendCallError() throws Exception {
162164
eventHandler.onCall(someId, null, null);
163165

164166
// then
165-
verify(communicator, times(1)).sendCallError(eq(someId), anyString(), anyString(), anyString());
167+
// TODO action should not be nullable
168+
verify(communicator).sendCallError(eq(someId), nullable(String.class), anyString(), anyString());
166169
}
167170

168171
@Test
@@ -178,7 +181,7 @@ public boolean validate() {
178181

179182
doAnswer(
180183
invocation ->
181-
invocation.getArgumentAt(0, CompletableFuture.class).complete(aConfirmation))
184+
invocation.getArgument(0, CompletableFuture.class).complete(aConfirmation))
182185
.when(fulfiller)
183186
.fulfill(any(), any(), any());
184187
when(communicator.unpackPayload(any(), any())).thenReturn(new TestRequest());
@@ -187,7 +190,8 @@ public boolean validate() {
187190
eventHandler.onCall(someId, null, null);
188191

189192
// then
190-
verify(communicator, times(1)).sendCallResult(anyString(), anyString(), eq(aConfirmation));
193+
// TODO action should not be nullable
194+
verify(communicator).sendCallResult(anyString(), nullable(String.class), eq(aConfirmation));
191195
}
192196

193197
@Test
@@ -197,7 +201,7 @@ public void onCall_callbackThrowsException_callSendCallResult() throws Exception
197201
doAnswer(
198202
invocation ->
199203
invocation
200-
.getArgumentAt(0, CompletableFuture.class)
204+
.getArgument(0, CompletableFuture.class)
201205
.completeExceptionally(new Exception()))
202206
.when(fulfiller)
203207
.fulfill(any(), any(), any());
@@ -207,7 +211,8 @@ public void onCall_callbackThrowsException_callSendCallResult() throws Exception
207211
eventHandler.onCall(someId, null, null);
208212

209213
// then
210-
verify(communicator, times(1)).sendCallError(eq(someId), anyString(), anyString(), anyString());
214+
// TODO uniqueid should not be nullable
215+
verify(communicator).sendCallError(eq(someId), nullable(String.class), anyString(), anyString());
211216
}
212217

213218
@Test
@@ -216,7 +221,7 @@ public void close_disconnects() {
216221
session.close();
217222

218223
// Then
219-
verify(communicator, times(1)).disconnect();
224+
verify(communicator).disconnect();
220225
}
221226

222227
@Test
@@ -229,6 +234,7 @@ public void onCall_unknownAction_callSendCallError() {
229234
eventHandler.onCall(someId, null, null);
230235

231236
// Then
232-
verify(communicator, times(1)).sendCallError(eq(someId), anyString(), anyString(), anyString());
237+
// TODO uniqueid should not be nullable
238+
verify(communicator).sendCallError(eq(someId), nullable(String.class), anyString(), anyString());
233239
}
234240
}

ocpp-common/src/test/java/eu/chargetime/ocpp/test/SimpleRequestDispatcherTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,20 +25,20 @@ of this software and associated documentation files (the "Software"), to deal
2525
SOFTWARE.
2626
*/
2727

28-
import static org.hamcrest.CoreMatchers.is;
29-
import static org.junit.Assert.assertThat;
30-
import static org.mockito.Matchers.any;
28+
import static org.junit.Assert.assertEquals;
29+
import static org.mockito.ArgumentMatchers.any;
3130
import static org.mockito.Mockito.when;
3231

3332
import eu.chargetime.ocpp.SessionEvents;
3433
import eu.chargetime.ocpp.SimplePromiseFulfiller;
3534
import eu.chargetime.ocpp.UnsupportedFeatureException;
3635
import eu.chargetime.ocpp.model.Confirmation;
3736
import java.util.concurrent.CompletableFuture;
37+
3838
import org.junit.Test;
3939
import org.junit.runner.RunWith;
4040
import org.mockito.Mock;
41-
import org.mockito.runners.MockitoJUnitRunner;
41+
import org.mockito.junit.MockitoJUnitRunner;
4242

4343
@RunWith(MockitoJUnitRunner.class)
4444
public class SimpleRequestDispatcherTest {
@@ -65,6 +65,6 @@ public void fulfill_throwsException_completesWithException() throws UnsupportedF
6565
sut.fulfill(promise, eventsMock, null);
6666

6767
// Then
68-
assertThat(result[0], is(expectedException));
68+
assertEquals(result[0], expectedException);
6969
}
7070
}

0 commit comments

Comments
 (0)