Skip to content

Commit c56ce42

Browse files
authored
RATIS-1977. Remove Junit 4 dependencies. (#1269)
1 parent 5528be0 commit c56ce42

File tree

11 files changed

+32
-78
lines changed

11 files changed

+32
-78
lines changed

pom.xml

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@
221221
<testsThreadCount>4</testsThreadCount>
222222

223223
<slf4j.version>2.0.7</slf4j.version>
224-
<junit-bom.version>5.11.2</junit-bom.version>
224+
<junit-bom.version>5.12.2</junit-bom.version>
225+
<mockito.version>4.11.0</mockito.version>
225226
<jacoco.version>0.8.12</jacoco.version>
227+
<jakarta.annotation.version>1.3.5</jakarta.annotation.version>
226228
<flaky-test-groups>flaky | org.apache.ratis.test.tag.FlakyTest</flaky-test-groups>
227229
</properties>
228230

@@ -417,12 +419,6 @@
417419
<scope>test</scope>
418420
<version>${slf4j.version}</version>
419421
</dependency>
420-
421-
<dependency>
422-
<groupId>junit</groupId>
423-
<artifactId>junit</artifactId>
424-
<version>4.13.2</version>
425-
</dependency>
426422
<dependency>
427423
<groupId>org.junit</groupId>
428424
<artifactId>junit-bom</artifactId>
@@ -433,13 +429,12 @@
433429
<dependency>
434430
<groupId>org.mockito</groupId>
435431
<artifactId>mockito-core</artifactId>
436-
<version>4.3.1</version>
432+
<version>${mockito.version}</version>
437433
</dependency>
438434
<dependency>
439-
<groupId>org.apache.tomcat</groupId>
440-
<artifactId>annotations-api</artifactId>
441-
<version>6.0.53</version>
442-
<scope>provided</scope>
435+
<groupId>jakarta.annotation</groupId>
436+
<artifactId>jakarta.annotation-api</artifactId>
437+
<version>${jakarta.annotation.version}</version>
443438
</dependency>
444439
</dependencies>
445440
</dependencyManagement>

ratis-common/pom.xml

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,6 @@
3838
<artifactId>slf4j-api</artifactId>
3939
</dependency>
4040

41-
<dependency>
42-
<groupId>junit</groupId>
43-
<artifactId>junit</artifactId>
44-
<scope>test</scope>
45-
</dependency>
46-
4741
<dependency>
4842
<groupId>org.junit.jupiter</groupId>
4943
<artifactId>junit-jupiter-api</artifactId>
@@ -54,11 +48,6 @@
5448
<artifactId>junit-jupiter-engine</artifactId>
5549
<scope>test</scope>
5650
</dependency>
57-
<dependency>
58-
<groupId>org.junit.vintage</groupId>
59-
<artifactId>junit-vintage-engine</artifactId>
60-
<scope>test</scope>
61-
</dependency>
6251
<dependency>
6352
<groupId>org.junit.platform</groupId>
6453
<artifactId>junit-platform-launcher</artifactId>

ratis-common/src/test/java/org/apache/ratis/BaseTest.java

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.apache.ratis.util.StringUtils;
2727
import org.apache.ratis.util.TimeDuration;
2828
import org.apache.ratis.util.function.CheckedRunnable;
29-
import org.junit.After;
30-
import org.junit.Before;
3129
import org.junit.jupiter.api.AfterEach;
3230
import org.junit.jupiter.api.Assertions;
3331
import org.junit.jupiter.api.Assumptions;
@@ -72,8 +70,6 @@ public void setFirstException(Throwable e) {
7270
}
7371
}
7472

75-
// TODO: Junit 4 reference should be removed once all the unit tests are migrated to Junit 5.
76-
7773
private String testCaseName;
7874

7975
@BeforeEach
@@ -85,8 +81,6 @@ public void setup(TestInfo testInfo) {
8581
+ "." + (method == null? null : method.getName());
8682
}
8783

88-
// @Before annotation is retained to support junit 4 tests.
89-
@Before
9084
@BeforeEach
9185
public void checkAssumptions() {
9286
final int leaks = ReferenceCountedLeakDetector.getLeakDetector().getLeakCount();
@@ -99,8 +93,6 @@ public void checkAssumptions() {
9993
Assumptions.assumeTrue(exited == null, () -> "Already exited with " + exited);
10094
}
10195

102-
// @After annotation is retained to support junit 4 tests.
103-
@After
10496
@AfterEach
10597
public void assertNoFailures() {
10698
final Throwable e = firstException.get();
@@ -133,7 +125,6 @@ public File getClassTestDir() {
133125
}
134126

135127
public File getTestDir() {
136-
// This will work for both junit 4 and 5.
137128
return new File(getClassTestDir(), testCaseName);
138129
}
139130

ratis-examples/pom.xml

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,6 @@
117117
<scope>runtime</scope>
118118
</dependency>
119119

120-
<dependency>
121-
<groupId>junit</groupId>
122-
<artifactId>junit</artifactId>
123-
<scope>test</scope>
124-
</dependency>
125120
<dependency>
126121
<groupId>org.junit.jupiter</groupId>
127122
<artifactId>junit-jupiter-engine</artifactId>

ratis-proto/pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,8 @@
176176
<artifactId>ratis-thirdparty-misc</artifactId>
177177
</dependency>
178178
<dependency>
179-
<groupId>org.apache.tomcat</groupId>
180-
<artifactId>annotations-api</artifactId>
179+
<groupId>jakarta.annotation</groupId>
180+
<artifactId>jakarta.annotation-api</artifactId>
181181
</dependency>
182182
</dependencies>
183183
</project>

ratis-server/pom.xml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,6 @@
6565
<artifactId>slf4j-api</artifactId>
6666
</dependency>
6767

68-
<dependency>
69-
<groupId>junit</groupId>
70-
<artifactId>junit</artifactId>
71-
<scope>test</scope>
72-
</dependency>
7368
<dependency>
7469
<groupId>org.junit.jupiter</groupId>
7570
<artifactId>junit-jupiter-api</artifactId>
@@ -80,11 +75,6 @@
8075
<artifactId>junit-jupiter-engine</artifactId>
8176
<scope>test</scope>
8277
</dependency>
83-
<dependency>
84-
<groupId>org.junit.vintage</groupId>
85-
<artifactId>junit-vintage-engine</artifactId>
86-
<scope>test</scope>
87-
</dependency>
8878
<dependency>
8979
<groupId>org.junit.platform</groupId>
9080
<artifactId>junit-platform-launcher</artifactId>

ratis-server/src/test/java/org/apache/ratis/RaftExceptionBaseTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434
import org.apache.ratis.util.Slf4jUtils;
3535
import org.apache.ratis.util.SizeInBytes;
3636
import org.junit.jupiter.api.Assertions;
37-
import org.junit.Assume;
37+
import org.junit.jupiter.api.Assumptions;
3838
import org.junit.jupiter.api.Test;
3939
import org.slf4j.event.Level;
4040

@@ -84,7 +84,7 @@ RaftClientReply assertNotLeaderException(RaftPeerId expectedSuggestedLeader,
8484
final SimpleMessage message = new SimpleMessage(messageId);
8585
final RaftClientReply reply = rpc.sendRequest(cluster.newRaftClientRequest(ClientId.randomId(), server, message));
8686
Assertions.assertNotNull(reply);
87-
Assume.assumeFalse(reply.isSuccess());
87+
Assumptions.assumeFalse(reply.isSuccess());
8888
final NotLeaderException nle = reply.getNotLeaderException();
8989
Objects.requireNonNull(nle);
9090
Assertions.assertEquals(expectedSuggestedLeader, nle.getSuggestedLeader().getId());

ratis-server/src/test/java/org/apache/ratis/RaftTestUtil.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,8 @@
4444
import org.apache.ratis.util.Preconditions;
4545
import org.apache.ratis.util.ProtoUtils;
4646
import org.apache.ratis.util.TimeDuration;
47-
import org.junit.AssumptionViolatedException;
47+
import org.apache.ratis.util.function.CheckedConsumer;
48+
import org.junit.jupiter.api.Assumptions;
4849
import org.junit.jupiter.api.Assertions;
4950
import org.slf4j.Logger;
5051
import org.slf4j.LoggerFactory;
@@ -479,18 +480,18 @@ static List<RaftPeer> getPeersWithPriority(List<RaftPeer> peers, RaftPeer sugges
479480

480481
static RaftPeerId changeLeader(MiniRaftCluster cluster, RaftPeerId oldLeader)
481482
throws Exception {
482-
return changeLeader(cluster, oldLeader, AssumptionViolatedException::new);
483+
return changeLeader(cluster, oldLeader, Assumptions::abort);
483484
}
484485

485-
static RaftPeerId changeLeader(MiniRaftCluster cluster, RaftPeerId oldLeader, Function<String, Exception> constructor)
486-
throws Exception {
486+
static RaftPeerId changeLeader(MiniRaftCluster cluster, RaftPeerId oldLeader,
487+
CheckedConsumer<String, Exception> failToChangeLeaderHandler) throws Exception {
487488
final String name = JavaUtils.getCallerStackTraceElement().getMethodName() + "-changeLeader";
488489
cluster.setBlockRequestsFrom(oldLeader.toString(), true);
489490
try {
490491
return JavaUtils.attemptRepeatedly(() -> {
491492
final RaftPeerId newLeader = waitForLeader(cluster).getId();
492493
if (newLeader.equals(oldLeader)) {
493-
throw constructor.apply("Failed to change leader: newLeader == oldLeader == " + oldLeader);
494+
failToChangeLeaderHandler.accept("Failed to change leader: newLeader == oldLeader == " + oldLeader);
494495
}
495496
LOG.info("Changed leader from " + oldLeader + " to " + newLeader);
496497
return newLeader;

ratis-server/src/test/java/org/apache/ratis/server/impl/LeaderElectionTests.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ public void testChangeLeader() throws Exception {
184184
void runTestChangeLeader(MiniRaftCluster cluster) throws Exception {
185185
RaftPeerId leader = RaftTestUtil.waitForLeader(cluster).getId();
186186
for(int i = 0; i < 10; i++) {
187-
leader = RaftTestUtil.changeLeader(cluster, leader, IllegalStateException::new);
187+
leader = RaftTestUtil.changeLeader(cluster, leader, Assertions::fail);
188188
}
189189
}
190190

ratis-server/src/test/java/org/apache/ratis/server/impl/StateMachineShutdownTests.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,10 @@
2828
import org.apache.ratis.statemachine.impl.SimpleStateMachine4Testing;
2929
import org.apache.ratis.statemachine.StateMachine;
3030
import org.apache.ratis.statemachine.TransactionContext;
31-
import org.junit.*;
31+
import org.junit.jupiter.api.AfterEach;
32+
import org.junit.jupiter.api.Assertions;
33+
import org.junit.jupiter.api.BeforeEach;
34+
import org.junit.jupiter.api.Test;
3235
import org.mockito.MockedStatic;
3336
import org.mockito.Mockito;
3437
import org.slf4j.Logger;
@@ -121,12 +124,12 @@ public void unblockAllTxns() {
121124
}
122125
}
123126

124-
@Before
127+
@BeforeEach
125128
public void setup() {
126129
mocked = Mockito.mockStatic(CompletableFuture.class, Mockito.CALLS_REAL_METHODS);
127130
}
128131

129-
@After
132+
@AfterEach
130133
public void tearDownClass() {
131134
if (mocked != null) {
132135
mocked.close();
@@ -162,10 +165,10 @@ public void testStateMachineShutdownWaitsForApplyTxn() throws Exception {
162165
RaftClientReply watchReply = client.io().watch(
163166
logIndex, RaftProtos.ReplicationLevel.ALL_COMMITTED);
164167
watchReply.getCommitInfos().forEach(
165-
val -> Assert.assertTrue(val.getCommitIndex() >= logIndex));
168+
val -> Assertions.assertTrue(val.getCommitIndex() >= logIndex));
166169
final RaftServer.Division secondFollower = cluster.getFollowers().get(1);
167170
// Second follower is blocked in apply transaction
168-
Assert.assertTrue(secondFollower.getInfo().getLastAppliedIndex() < logIndex);
171+
Assertions.assertTrue(secondFollower.getInfo().getLastAppliedIndex() < logIndex);
169172

170173
// Now shutdown the follower in a separate thread
171174
final Thread t = new Thread(secondFollower::close);
@@ -176,24 +179,24 @@ public void testStateMachineShutdownWaitsForApplyTxn() throws Exception {
176179
// Now unblock the second follower
177180
long minIndex = ((StateMachineWithConditionalWait) secondFollower.getStateMachine()).blockTxns.stream()
178181
.min(Comparator.naturalOrder()).get();
179-
Assert.assertEquals(2, StateMachineWithConditionalWait.numTxns.values().stream()
182+
Assertions.assertEquals(2, StateMachineWithConditionalWait.numTxns.values().stream()
180183
.filter(val -> val.get() == 3).count());
181184
// The second follower should still be blocked in apply transaction
182-
Assert.assertTrue(secondFollower.getInfo().getLastAppliedIndex() < minIndex);
185+
Assertions.assertTrue(secondFollower.getInfo().getLastAppliedIndex() < minIndex);
183186
for (long index : ((StateMachineWithConditionalWait) secondFollower.getStateMachine()).blockTxns) {
184187
if (minIndex != index) {
185188
((StateMachineWithConditionalWait) secondFollower.getStateMachine()).unBlockApplyTxn(index);
186189
}
187190
}
188-
Assert.assertEquals(2, StateMachineWithConditionalWait.numTxns.values().stream()
191+
Assertions.assertEquals(2, StateMachineWithConditionalWait.numTxns.values().stream()
189192
.filter(val -> val.get() == 3).count());
190-
Assert.assertTrue(secondFollower.getInfo().getLastAppliedIndex() < minIndex);
193+
Assertions.assertTrue(secondFollower.getInfo().getLastAppliedIndex() < minIndex);
191194
((StateMachineWithConditionalWait) secondFollower.getStateMachine()).unBlockApplyTxn(minIndex);
192195

193196
// Now wait for the thread
194197
t.join(5000);
195-
Assert.assertEquals(logIndex, secondFollower.getInfo().getLastAppliedIndex());
196-
Assert.assertEquals(3, StateMachineWithConditionalWait.numTxns.values().stream()
198+
Assertions.assertEquals(logIndex, secondFollower.getInfo().getLastAppliedIndex());
199+
Assertions.assertEquals(3, StateMachineWithConditionalWait.numTxns.values().stream()
197200
.filter(val -> val.get() == 3).count());
198201

199202
cluster.shutdown();

0 commit comments

Comments
 (0)