Skip to content

Commit 346a975

Browse files
KarboniteKreamtrustin
authored andcommitted
Migrate unit tests to JUnit 5 (Part 1) (#471)
This PR migrates part of unit tests to JUnit 5. #### Changes: - Migrate some tests to JUnit 5 - Use nested tests in `ProjectServiceV1Test` to reduce the number of Central Dogma instances - Upgrade Mockito to 3.2.4 and revert an earlier change in `ZooKeeperCommandExecutorTest` - Use `assertThat().hasSize()` where applicable
1 parent fe81f2a commit 346a975

File tree

70 files changed

+646
-658
lines changed

Some content is hidden

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

70 files changed

+646
-658
lines changed

build.gradle

+1
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ configure(projectsWithFlags('java')) {
9191
testCompile 'org.hamcrest:hamcrest-library'
9292
testCompile 'org.assertj:assertj-core'
9393
testCompile 'org.mockito:mockito-core'
94+
testCompile 'org.mockito:mockito-junit-jupiter'
9495
testCompile 'org.junit.jupiter:junit-jupiter-api'
9596
testCompile 'org.junit.jupiter:junit-jupiter-params'
9697
testRuntime 'org.junit.jupiter:junit-jupiter-engine'

client/java-armeria-legacy/src/test/java/com/linecorp/centraldogma/client/armeria/legacy/LegacyCentralDogmaTest.java

+34-57
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 LINE Corporation
2+
* Copyright 2020 LINE Corporation
33
*
44
* LINE Corporation licenses this file to you under the Apache License,
55
* version 2.0 (the "License"); you may not use this file except in compliance
@@ -28,12 +28,9 @@
2828
import java.util.Set;
2929

3030
import org.apache.thrift.async.AsyncMethodCallback;
31-
import org.junit.Before;
32-
import org.junit.Rule;
33-
import org.junit.Test;
31+
import org.junit.jupiter.api.BeforeEach;
32+
import org.junit.jupiter.api.Test;
3433
import org.mockito.Mock;
35-
import org.mockito.junit.MockitoJUnit;
36-
import org.mockito.junit.MockitoRule;
3734

3835
import com.google.common.collect.ImmutableList;
3936
import com.google.common.collect.ImmutableMap;
@@ -72,24 +69,22 @@
7269
import com.linecorp.centraldogma.internal.thrift.WatchFileResult;
7370
import com.linecorp.centraldogma.internal.thrift.WatchRepositoryResult;
7471

75-
public class LegacyCentralDogmaTest {
76-
private static final String TIMESTAMP = "2016-01-02T03:04:05Z";
72+
class LegacyCentralDogmaTest {
7773

78-
@Rule
79-
public MockitoRule rule = MockitoJUnit.rule();
74+
private static final String TIMESTAMP = "2016-01-02T03:04:05Z";
8075

8176
@Mock
8277
private CentralDogmaService.AsyncIface iface;
8378

8479
private CentralDogma client;
8580

86-
@Before
87-
public void setup() {
81+
@BeforeEach
82+
void setUp() {
8883
client = new LegacyCentralDogma(CommonPools.workerGroup(), iface);
8984
}
9085

9186
@Test
92-
public void createProject() throws Exception {
87+
void createProject() throws Exception {
9388
doAnswer(invocation -> {
9489
final AsyncMethodCallback<Void> callback = invocation.getArgument(1);
9590
callback.onComplete(null);
@@ -100,7 +95,7 @@ public void createProject() throws Exception {
10095
}
10196

10297
@Test
103-
public void removeProject() throws Exception {
98+
void removeProject() throws Exception {
10499
doAnswer(invocation -> {
105100
final AsyncMethodCallback<Void> callback = invocation.getArgument(1);
106101
callback.onComplete(null);
@@ -111,7 +106,7 @@ public void removeProject() throws Exception {
111106
}
112107

113108
@Test
114-
public void purgeProject() throws Exception {
109+
void purgeProject() throws Exception {
115110
doAnswer(invocation -> {
116111
final AsyncMethodCallback<Void> callback = invocation.getArgument(1);
117112
callback.onComplete(null);
@@ -122,7 +117,7 @@ public void purgeProject() throws Exception {
122117
}
123118

124119
@Test
125-
public void unremoveProject() throws Exception {
120+
void unremoveProject() throws Exception {
126121
doAnswer(invocation -> {
127122
final AsyncMethodCallback<Void> callback = invocation.getArgument(1);
128123
callback.onComplete(null);
@@ -133,7 +128,7 @@ public void unremoveProject() throws Exception {
133128
}
134129

135130
@Test
136-
public void listProjects() throws Exception {
131+
void listProjects() throws Exception {
137132
doAnswer(invocation -> {
138133
final AsyncMethodCallback<List<Project>> callback = invocation.getArgument(0);
139134
callback.onComplete(ImmutableList.of(new Project("project")));
@@ -144,7 +139,7 @@ public void listProjects() throws Exception {
144139
}
145140

146141
@Test
147-
public void listRemovedProjects() throws Exception {
142+
void listRemovedProjects() throws Exception {
148143
doAnswer(invocation -> {
149144
final AsyncMethodCallback<Set<String>> callback = invocation.getArgument(0);
150145
callback.onComplete(ImmutableSet.of("project"));
@@ -155,7 +150,7 @@ public void listRemovedProjects() throws Exception {
155150
}
156151

157152
@Test
158-
public void createRepository() throws Exception {
153+
void createRepository() throws Exception {
159154
doAnswer(invocation -> {
160155
final AsyncMethodCallback<Void> callback = invocation.getArgument(2);
161156
callback.onComplete(null);
@@ -166,7 +161,7 @@ public void createRepository() throws Exception {
166161
}
167162

168163
@Test
169-
public void removeRepository() throws Exception {
164+
void removeRepository() throws Exception {
170165
doAnswer(invocation -> {
171166
final AsyncMethodCallback<Void> callback = invocation.getArgument(2);
172167
callback.onComplete(null);
@@ -177,7 +172,7 @@ public void removeRepository() throws Exception {
177172
}
178173

179174
@Test
180-
public void purgeRepository() throws Exception {
175+
void purgeRepository() throws Exception {
181176
doAnswer(invocation -> {
182177
final AsyncMethodCallback<Void> callback = invocation.getArgument(2);
183178
callback.onComplete(null);
@@ -188,7 +183,7 @@ public void purgeRepository() throws Exception {
188183
}
189184

190185
@Test
191-
public void unremoveRepository() throws Exception {
186+
void unremoveRepository() throws Exception {
192187
doAnswer(invocation -> {
193188
final AsyncMethodCallback<Void> callback = invocation.getArgument(2);
194189
callback.onComplete(null);
@@ -199,7 +194,7 @@ public void unremoveRepository() throws Exception {
199194
}
200195

201196
@Test
202-
public void listRepositories() throws Exception {
197+
void listRepositories() throws Exception {
203198
doAnswer(invocation -> {
204199
final AsyncMethodCallback<List<Repository>> callback = invocation.getArgument(1);
205200
final Repository repository = new Repository("repo").setHead(
@@ -216,7 +211,7 @@ public void listRepositories() throws Exception {
216211
}
217212

218213
@Test
219-
public void listRemovedRepositories() throws Exception {
214+
void listRemovedRepositories() throws Exception {
220215
doAnswer(invocation -> {
221216
final AsyncMethodCallback<Set<String>> callback = invocation.getArgument(1);
222217
callback.onComplete(ImmutableSet.of("repo"));
@@ -227,7 +222,7 @@ public void listRemovedRepositories() throws Exception {
227222
}
228223

229224
@Test
230-
public void normalizeRevision() throws Exception {
225+
void normalizeRevision() throws Exception {
231226
doAnswer(invocation -> {
232227
final AsyncMethodCallback<TRevision> callback = invocation.getArgument(3);
233228
callback.onComplete(new TRevision(3));
@@ -239,7 +234,7 @@ public void normalizeRevision() throws Exception {
239234
}
240235

241236
@Test
242-
public void listFiles() throws Exception {
237+
void listFiles() throws Exception {
243238
doAnswer(invocation -> {
244239
final AsyncMethodCallback<List<TEntry>> callback = invocation.getArgument(4);
245240
final TEntry entry = new TEntry("/a.txt", TEntryType.TEXT);
@@ -253,13 +248,7 @@ public void listFiles() throws Exception {
253248
}
254249

255250
@Test
256-
public void getFiles() throws Exception {
257-
doAnswer(invocation -> {
258-
final AsyncMethodCallback<com.linecorp.centraldogma.internal.thrift.Revision> callback =
259-
invocation.getArgument(3);
260-
callback.onComplete(new com.linecorp.centraldogma.internal.thrift.Revision(1, 0));
261-
return null;
262-
}).when(iface).normalizeRevision(any(), any(), any(), any());
251+
void getFiles() throws Exception {
263252
doAnswer(invocation -> {
264253
final AsyncMethodCallback<List<TEntry>> callback = invocation.getArgument(4);
265254
final TEntry entry = new TEntry("/b.txt", TEntryType.TEXT);
@@ -273,7 +262,7 @@ public void getFiles() throws Exception {
273262
}
274263

275264
@Test
276-
public void getHistory() throws Exception {
265+
void getHistory() throws Exception {
277266
doAnswer(invocation -> {
278267
final AsyncMethodCallback<List<TCommit>> callback = invocation.getArgument(5);
279268
callback.onComplete(ImmutableList.of(new TCommit(
@@ -294,7 +283,7 @@ public void getHistory() throws Exception {
294283
}
295284

296285
@Test
297-
public void getDiffs() throws Exception {
286+
void getDiffs() throws Exception {
298287
doAnswer(invocation -> {
299288
final AsyncMethodCallback<List<TChange>> callback = invocation.getArgument(5);
300289
final TChange change = new TChange("/a.txt", ChangeType.UPSERT_TEXT);
@@ -308,7 +297,7 @@ public void getDiffs() throws Exception {
308297
}
309298

310299
@Test
311-
public void getPreviewDiffs() throws Exception {
300+
void getPreviewDiffs() throws Exception {
312301
doAnswer(invocation -> {
313302
final AsyncMethodCallback<List<TChange>> callback = invocation.getArgument(4);
314303
final TChange change = new TChange("/a.txt", ChangeType.UPSERT_TEXT);
@@ -323,7 +312,7 @@ public void getPreviewDiffs() throws Exception {
323312
}
324313

325314
@Test
326-
public void push() throws Exception {
315+
void push() throws Exception {
327316
doAnswer(invocation -> {
328317
final AsyncMethodCallback<TCommit> callback = invocation.getArgument(7);
329318
callback.onComplete(new TCommit(
@@ -345,13 +334,7 @@ public void push() throws Exception {
345334
}
346335

347336
@Test
348-
public void getFile() throws Exception {
349-
doAnswer(invocation -> {
350-
final AsyncMethodCallback<com.linecorp.centraldogma.internal.thrift.Revision> callback =
351-
invocation.getArgument(3);
352-
callback.onComplete(new com.linecorp.centraldogma.internal.thrift.Revision(1, 0));
353-
return null;
354-
}).when(iface).normalizeRevision(any(), any(), any(), any());
337+
void getFile() throws Exception {
355338
doAnswer(invocation -> {
356339
final AsyncMethodCallback<GetFileResult> callback = invocation.getArgument(4);
357340
callback.onComplete(new GetFileResult(TEntryType.TEXT, "content"));
@@ -363,13 +346,7 @@ public void getFile() throws Exception {
363346
}
364347

365348
@Test
366-
public void getFile_path() throws Exception {
367-
doAnswer(invocation -> {
368-
final AsyncMethodCallback<com.linecorp.centraldogma.internal.thrift.Revision> callback =
369-
invocation.getArgument(3);
370-
callback.onComplete(new com.linecorp.centraldogma.internal.thrift.Revision(1, 0));
371-
return null;
372-
}).when(iface).normalizeRevision(any(), any(), any(), any());
349+
void getFile_path() throws Exception {
373350
doAnswer(invocation -> {
374351
final AsyncMethodCallback<GetFileResult> callback = invocation.getArgument(4);
375352
callback.onComplete(new GetFileResult(TEntryType.TEXT, "content"));
@@ -381,7 +358,7 @@ public void getFile_path() throws Exception {
381358
}
382359

383360
@Test
384-
public void mergeFiles() throws Exception {
361+
void mergeFiles() throws Exception {
385362
doAnswer(invocation -> {
386363
final AsyncMethodCallback<MergedEntry> callback = invocation.getArgument(4);
387364
callback.onComplete(new MergedEntry(new TRevision(1), TEntryType.JSON, "{\"foo\": \"bar\"}",
@@ -399,7 +376,7 @@ public void mergeFiles() throws Exception {
399376
}
400377

401378
@Test
402-
public void diffFile() throws Exception {
379+
void diffFile() throws Exception {
403380
doAnswer(invocation -> {
404381
final AsyncMethodCallback<DiffFileResult> callback = invocation.getArgument(5);
405382
callback.onComplete(new DiffFileResult(ChangeType.UPSERT_TEXT, "some_text"));
@@ -412,7 +389,7 @@ public void diffFile() throws Exception {
412389
}
413390

414391
@Test
415-
public void watchRepository() throws Exception {
392+
void watchRepository() throws Exception {
416393
doAnswer(invocation -> {
417394
final AsyncMethodCallback<WatchRepositoryResult> callback = invocation.getArgument(5);
418395
callback.onComplete(new WatchRepositoryResult().setRevision(new TRevision(42)));
@@ -424,7 +401,7 @@ public void watchRepository() throws Exception {
424401
}
425402

426403
@Test
427-
public void watchRepositoryTimedOut() throws Exception {
404+
void watchRepositoryTimedOut() throws Exception {
428405
doAnswer(invocation -> {
429406
AsyncMethodCallback<WatchRepositoryResult> callback = invocation.getArgument(5);
430407
callback.onComplete(new WatchRepositoryResult());
@@ -436,7 +413,7 @@ public void watchRepositoryTimedOut() throws Exception {
436413
}
437414

438415
@Test
439-
public void watchFile() throws Exception {
416+
void watchFile() throws Exception {
440417
doAnswer(invocation -> {
441418
AsyncMethodCallback<WatchFileResult> callback = invocation.getArgument(5);
442419
callback.onComplete(new WatchFileResult().setRevision(new TRevision(42))
@@ -450,7 +427,7 @@ public void watchFile() throws Exception {
450427
}
451428

452429
@Test
453-
public void watchFileTimedOut() throws Exception {
430+
void watchFileTimedOut() throws Exception {
454431
doAnswer(invocation -> {
455432
AsyncMethodCallback<WatchFileResult> callback = invocation.getArgument(5);
456433
callback.onComplete(new WatchFileResult());

client/java-armeria-legacy/src/test/java/com/linecorp/centraldogma/client/armeria/legacy/LegacyCentralDogmaTimeoutSchedulerTest.java

+11-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2018 LINE Corporation
2+
* Copyright 2020 LINE Corporation
33
*
44
* LINE Corporation licenses this file to you under the Apache License,
55
* version 2.0 (the "License"); you may not use this file except in compliance
@@ -22,12 +22,9 @@
2222

2323
import java.util.List;
2424

25-
import org.junit.Before;
26-
import org.junit.Rule;
27-
import org.junit.Test;
25+
import org.junit.jupiter.api.BeforeEach;
26+
import org.junit.jupiter.api.Test;
2827
import org.mockito.Mock;
29-
import org.mockito.junit.MockitoJUnit;
30-
import org.mockito.junit.MockitoRule;
3128

3229
import com.google.common.collect.ImmutableList;
3330

@@ -40,42 +37,40 @@
4037
import com.linecorp.centraldogma.internal.api.v1.WatchTimeout;
4138
import com.linecorp.centraldogma.internal.thrift.CentralDogmaService;
4239

43-
public class LegacyCentralDogmaTimeoutSchedulerTest {
44-
@Rule
45-
public MockitoRule rule = MockitoJUnit.rule();
40+
class LegacyCentralDogmaTimeoutSchedulerTest {
4641

4742
@Mock
4843
private RpcClient client;
4944

5045
private LegacyCentralDogmaTimeoutScheduler decorator;
5146

52-
@Before
53-
public void setup() {
47+
@BeforeEach
48+
void setUp() {
5449
decorator = new LegacyCentralDogmaTimeoutScheduler(client);
5550
}
5651

5752
@Test
58-
public void execute() throws Exception {
53+
void execute() throws Exception {
5954
check("listProjects", 1000L, 1L, 1L);
6055
}
6156

6257
@Test
63-
public void execute_watchFile() throws Exception {
58+
void execute_watchFile() throws Exception {
6459
check("watchFile", 1000L, 1L, 1001L);
6560
}
6661

6762
@Test
68-
public void execute_watchRepository() throws Exception {
63+
void execute_watchRepository() throws Exception {
6964
check("watchRepository", 1000L, 1L, 1001L);
7065
}
7166

7267
@Test
73-
public void execute_watch_timeoutOverflow() throws Exception {
68+
void execute_watch_timeoutOverflow() throws Exception {
7469
check("watchRepository", Long.MAX_VALUE - 10, 100L, WatchTimeout.MAX_MILLIS);
7570
}
7671

7772
@Test
78-
public void execute_noTimeout() throws Exception {
73+
void execute_noTimeout() throws Exception {
7974
check("watchFile", 1000L, 0, 0);
8075
}
8176

0 commit comments

Comments
 (0)