Skip to content

Commit ce61432

Browse files
Migrate tests to JUnit5 (#86)
1 parent e63498f commit ce61432

File tree

3 files changed

+47
-66
lines changed

3 files changed

+47
-66
lines changed

pom.xml

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -120,26 +120,11 @@
120120
</dependency>
121121

122122
<!-- Test dependencies -->
123-
<dependency>
124-
<groupId>junit</groupId>
125-
<artifactId>junit</artifactId>
126-
<scope>test</scope>
127-
</dependency>
128-
<dependency>
129-
<groupId>org.junit.vintage</groupId>
130-
<artifactId>junit-vintage-engine</artifactId>
131-
<scope>test</scope>
132-
</dependency>
133123
<dependency>
134124
<groupId>org.glassfish.tyrus.bundles</groupId>
135125
<artifactId>tyrus-standalone-client</artifactId>
136126
<scope>test</scope>
137127
</dependency>
138-
<dependency>
139-
<groupId>org.mockito</groupId>
140-
<artifactId>mockito-core</artifactId>
141-
<scope>test</scope>
142-
</dependency>
143128
<dependency>
144129
<groupId>org.springframework.boot</groupId>
145130
<artifactId>spring-boot-starter-test</artifactId>

src/test/java/org/gridsuite/study/notification/server/NotificationWebSocketHandlerTest.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -6,23 +6,16 @@
66
*/
77
package org.gridsuite.study.notification.server;
88

9-
import java.time.Duration;
10-
import java.util.*;
11-
import java.util.concurrent.ConcurrentHashMap;
12-
import java.util.concurrent.atomic.AtomicReference;
13-
import java.util.function.Function;
14-
import java.util.stream.Collectors;
15-
import java.util.stream.Stream;
16-
179
import com.fasterxml.jackson.core.JsonProcessingException;
1810
import com.fasterxml.jackson.databind.ObjectMapper;
1911
import com.fasterxml.jackson.databind.ObjectWriter;
20-
2112
import io.micrometer.core.instrument.MeterRegistry;
2213
import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
23-
import org.gridsuite.study.notification.server.dto.*;
24-
import org.junit.Before;
25-
import org.junit.Test;
14+
import org.gridsuite.study.notification.server.dto.Filters;
15+
import org.gridsuite.study.notification.server.dto.FiltersToAdd;
16+
import org.gridsuite.study.notification.server.dto.FiltersToRemove;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.junit.jupiter.api.Test;
2619
import org.mockito.ArgumentCaptor;
2720
import org.mockito.Mockito;
2821
import org.springframework.core.io.buffer.DataBuffer;
@@ -40,15 +33,23 @@
4033
import reactor.core.publisher.FluxSink;
4134
import reactor.core.publisher.Mono;
4235

36+
import java.time.Duration;
37+
import java.util.*;
38+
import java.util.concurrent.ConcurrentHashMap;
39+
import java.util.concurrent.atomic.AtomicReference;
40+
import java.util.function.Function;
41+
import java.util.stream.Collectors;
42+
import java.util.stream.Stream;
43+
4344
import static org.gridsuite.study.notification.server.NotificationWebSocketHandler.*;
44-
import static org.junit.Assert.*;
45+
import static org.junit.jupiter.api.Assertions.*;
4546
import static org.mockito.ArgumentMatchers.any;
4647
import static org.mockito.Mockito.*;
4748

4849
/**
4950
* @author Jon Harper <jon.harper at rte-france.com>
5051
*/
51-
public class NotificationWebSocketHandlerTest {
52+
class NotificationWebSocketHandlerTest {
5253

5354
private ObjectMapper objectMapper;
5455
private WebSocketSession ws;
@@ -57,8 +58,8 @@ public class NotificationWebSocketHandlerTest {
5758

5859
private final MeterRegistry meterRegistry = new SimpleMeterRegistry();
5960

60-
@Before
61-
public void setup() {
61+
@BeforeEach
62+
void setup() {
6263
objectMapper = new ObjectMapper();
6364
var dataBufferFactory = new DefaultDataBufferFactory();
6465

@@ -193,7 +194,7 @@ private void withFilters(String filterStudyUuid, String filterUpdateType, boolea
193194
return (filterStudyUuid == null || filterStudyUuid.equals(studyUuid)) && (filterUpdateType == null || filterUpdateType.equals(updateType));
194195
})
195196
.map(GenericMessage::getHeaders)
196-
.map(this::toResultHeader)
197+
.map(NotificationWebSocketHandlerTest::toResultHeader)
197198
.collect(Collectors.toList());
198199

199200
List<Map<String, Object>> actual = messages.stream().map(t -> {
@@ -207,7 +208,7 @@ private void withFilters(String filterStudyUuid, String filterUpdateType, boolea
207208
assertNotEquals(0, actual.size());
208209
}
209210

210-
private Map<String, Object> toResultHeader(Map<String, Object> messageHeader) {
211+
private static Map<String, Object> toResultHeader(Map<String, Object> messageHeader) {
211212
var resHeader = new HashMap<String, Object>();
212213
resHeader.put(HEADER_TIMESTAMP, messageHeader.get(HEADER_TIMESTAMP));
213214
resHeader.put(HEADER_UPDATE_TYPE, messageHeader.get(HEADER_UPDATE_TYPE));
@@ -228,64 +229,64 @@ private Map<String, Object> toResultHeader(Map<String, Object> messageHeader) {
228229
return resHeader;
229230
}
230231

231-
private void passHeaderRef(Map<String, Object> messageHeader, HashMap<String, Object> resHeader, String headerName) {
232+
private static void passHeaderRef(Map<String, Object> messageHeader, HashMap<String, Object> resHeader, String headerName) {
232233
if (messageHeader.get(headerName) != null) {
233234
resHeader.put(headerName, messageHeader.get(headerName));
234235
}
235236
}
236237

237238
@Test
238-
public void testWithoutFilterInBody() {
239+
void testWithoutFilterInBody() {
239240
withFilters(null, null, false);
240241
}
241242

242243
@Test
243-
public void testWithoutFilterInUrl() {
244+
void testWithoutFilterInUrl() {
244245
withFilters(null, null, true);
245246
}
246247

247248
@Test
248-
public void testStudyFilterInBody() {
249+
void testStudyFilterInBody() {
249250
withFilters("bar", null, false);
250251
}
251252

252253
@Test
253-
public void testStudyFilterInUrl() {
254+
void testStudyFilterInUrl() {
254255
withFilters("bar", null, true);
255256
}
256257

257258
@Test
258-
public void testTypeFilterInBody() {
259+
void testTypeFilterInBody() {
259260
withFilters(null, "rab", false);
260261
}
261262

262263
@Test
263-
public void testTypeFilterInUrl() {
264+
void testTypeFilterInUrl() {
264265
withFilters(null, "rab", true);
265266
}
266267

267268
@Test
268-
public void testStudyAndTypeFilterInBody() {
269+
void testStudyAndTypeFilterInBody() {
269270
withFilters("bar", "rab", false);
270271
}
271272

272273
@Test
273-
public void testStudyAndTypeFilterInUrl() {
274+
void testStudyAndTypeFilterInUrl() {
274275
withFilters("bar", "rab", true);
275276
}
276277

277278
@Test
278-
public void testEncodingCharactersInBody() {
279+
void testEncodingCharactersInBody() {
279280
withFilters("foo bar/bar", "foobar", false);
280281
}
281282

282283
@Test
283-
public void testEncodingCharactersInUrl() {
284+
void testEncodingCharactersInUrl() {
284285
withFilters("foo bar/bar", "foobar", true);
285286
}
286287

287288
@Test
288-
public void testWsReceiveFilters() throws JsonProcessingException {
289+
void testWsReceiveFilters() throws Exception {
289290
setUpUriComponentBuilder("userId");
290291
var dataBufferFactory = new DefaultDataBufferFactory();
291292

@@ -308,7 +309,7 @@ public void testWsReceiveFilters() throws JsonProcessingException {
308309
}
309310

310311
@Test
311-
public void testWsRemoveFilters() throws JsonProcessingException {
312+
void testWsRemoveFilters() throws Exception {
312313
setUpUriComponentBuilder("userId");
313314
var dataBufferFactory = new DefaultDataBufferFactory();
314315

@@ -335,7 +336,7 @@ public void testWsRemoveFilters() throws JsonProcessingException {
335336
}
336337

337338
@Test
338-
public void testWsReceiveEmptyFilters() throws JsonProcessingException {
339+
void testWsReceiveEmptyFilters() throws Exception {
339340
setUpUriComponentBuilder("userId");
340341
var dataBufferFactory = new DefaultDataBufferFactory();
341342

@@ -356,7 +357,7 @@ public void testWsReceiveEmptyFilters() throws JsonProcessingException {
356357
}
357358

358359
@Test
359-
public void testWsReceiveUnprocessableFilter() {
360+
void testWsReceiveUnprocessableFilter() {
360361
setUpUriComponentBuilder("userId");
361362
var dataBufferFactory = new DefaultDataBufferFactory();
362363

@@ -374,7 +375,7 @@ public void testWsReceiveUnprocessableFilter() {
374375
}
375376

376377
@Test
377-
public void testHeartbeat() {
378+
void testHeartbeat() {
378379
setUpUriComponentBuilder("userId");
379380

380381
var notificationWebSocketHandler = new NotificationWebSocketHandler(null, meterRegistry, 1);
@@ -389,7 +390,7 @@ public void testHeartbeat() {
389390
}
390391

391392
@Test
392-
public void testDiscard() {
393+
void testDiscard() {
393394
setUpUriComponentBuilder("userId");
394395

395396
var notificationWebSocketHandler = new NotificationWebSocketHandler(objectMapper, meterRegistry, Integer.MAX_VALUE);

src/test/java/org/gridsuite/study/notification/server/NotificationWebSocketIT.java

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -7,16 +7,13 @@
77
package org.gridsuite.study.notification.server;
88

99
import io.micrometer.core.instrument.MeterRegistry;
10-
import org.junit.Ignore;
11-
import org.junit.Test;
12-
import org.junit.runner.RunWith;
10+
import org.junit.jupiter.api.Disabled;
11+
import org.junit.jupiter.api.Test;
1312
import org.springframework.beans.factory.annotation.Autowired;
1413
import org.springframework.boot.test.context.SpringBootTest;
1514
import org.springframework.boot.test.web.server.LocalServerPort;
1615
import org.springframework.http.HttpHeaders;
1716
import org.springframework.test.annotation.DirtiesContext;
18-
import org.springframework.test.context.ContextConfiguration;
19-
import org.springframework.test.context.junit4.SpringRunner;
2017
import org.springframework.web.reactive.socket.client.StandardWebSocketClient;
2118
import org.springframework.web.reactive.socket.client.WebSocketClient;
2219
import reactor.core.publisher.Mono;
@@ -28,16 +25,14 @@
2825
import java.util.concurrent.CountDownLatch;
2926

3027
import static org.gridsuite.study.notification.server.NotificationWebSocketHandler.*;
31-
import static org.junit.Assert.assertEquals;
28+
import static org.junit.jupiter.api.Assertions.assertEquals;
3229

3330
/**
3431
* @author Jon Harper <jon.harper at rte-france.com>
3532
*/
36-
@RunWith(SpringRunner.class)
37-
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
38-
@ContextConfiguration(classes = { NotificationApplication.class })
33+
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT, classes = {NotificationApplication.class})
3934
@DirtiesContext
40-
public class NotificationWebSocketIT {
35+
class NotificationWebSocketIT {
4136

4237
@LocalServerPort
4338
private String port;
@@ -46,7 +41,7 @@ public class NotificationWebSocketIT {
4641
private MeterRegistry meterRegistry;
4742

4843
@Test
49-
public void echo() {
44+
void echo() {
5045
WebSocketClient client = new StandardWebSocketClient();
5146
HttpHeaders httpHeaders = new HttpHeaders();
5247
httpHeaders.add(HEADER_USER_ID, "test");
@@ -58,8 +53,8 @@ protected URI getUrl(String path) {
5853
}
5954

6055
@Test
61-
@Ignore("This test case is not stable due to unexpected behavior of meterRegistry in asynchronous test context")
62-
public void metricsMapOneUserTwoConnections() {
56+
@Disabled("This test case is not stable due to unexpected behavior of meterRegistry in asynchronous test context")
57+
void metricsMapOneUserTwoConnections() {
6358
WebSocketClient client1 = new StandardWebSocketClient();
6459
HttpHeaders httpHeaders1 = new HttpHeaders();
6560
String user = "test";
@@ -77,8 +72,8 @@ public void metricsMapOneUserTwoConnections() {
7772
}
7873

7974
@Test
80-
@Ignore("This test case is not stable due to unexpected behavior of meterRegistry in asynchronous test context")
81-
public void metricsMapTwoUsers() {
75+
@Disabled("This test case is not stable due to unexpected behavior of meterRegistry in asynchronous test context")
76+
void metricsMapTwoUsers() {
8277
// First WebSocketClient for connections related to 'test' user
8378
WebSocketClient client1 = new StandardWebSocketClient();
8479
HttpHeaders httpHeaders1 = new HttpHeaders();
@@ -102,7 +97,7 @@ public void metricsMapTwoUsers() {
10297
evaluationFuture.join(); // Throw assertion errors
10398
}
10499

105-
private void handleLatches(CountDownLatch connectionLatch, CountDownLatch assertLatch) {
100+
private static void handleLatches(CountDownLatch connectionLatch, CountDownLatch assertLatch) {
106101
try {
107102
connectionLatch.countDown();
108103
assertLatch.await(); // Wait for assertion to be evaluated before closing the connection

0 commit comments

Comments
 (0)