Skip to content

Commit 82b7a64

Browse files
author
fanshilun
committed
RATIS-2124. Remove the use of org.junit.After/Rule.
1 parent 59f1992 commit 82b7a64

File tree

3 files changed

+46
-14
lines changed

3 files changed

+46
-14
lines changed

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,13 @@
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;
31-
import org.junit.Rule;
3229
import org.junit.jupiter.api.AfterEach;
3330
import org.junit.jupiter.api.Assertions;
3431
import org.junit.jupiter.api.Assumptions;
3532
import org.junit.jupiter.api.BeforeEach;
3633
import org.junit.jupiter.api.TestInfo;
3734
import org.junit.jupiter.api.Timeout;
38-
import org.junit.rules.TestName;
35+
import org.junit.jupiter.api.extension.RegisterExtension;
3936
import org.slf4j.Logger;
4037
import org.slf4j.LoggerFactory;
4138
import org.slf4j.event.Level;
@@ -87,8 +84,7 @@ public void setup(TestInfo testInfo) {
8784
+ "." + (method == null? null : method.getName());
8885
}
8986

90-
// @Before annotation is retained to support junit 4 tests.
91-
@Before
87+
@BeforeEach
9288
public void checkAssumptions() {
9389
final int leaks = ReferenceCountedLeakDetector.getLeakDetector().getLeakCount();
9490
Assumptions.assumeFalse(0 < leaks, () -> "numLeaks " + leaks + " > 0");
@@ -100,8 +96,6 @@ public void checkAssumptions() {
10096
Assumptions.assumeTrue(exited == null, () -> "Already exited with " + exited);
10197
}
10298

103-
// @After annotation is retained to support junit 4 tests.
104-
@After
10599
@AfterEach
106100
public void assertNoFailures() {
107101
final Throwable e = firstException.get();
@@ -112,8 +106,7 @@ public void assertNoFailures() {
112106
ExitUtils.assertNotTerminated();
113107
}
114108

115-
// Retained to support junit 4 tests.
116-
@Rule
109+
@RegisterExtension
117110
public final TestName testName = new TestName();
118111

119112
private static final Supplier<File> ROOT_TEST_DIR = JavaUtils.memoize(
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
package org.apache.ratis;
19+
20+
import org.junit.jupiter.api.extension.BeforeEachCallback;
21+
import org.junit.jupiter.api.extension.ExtensionContext;
22+
23+
/**
24+
* This is a custom JUnit5 `RegisterExtension`
25+
* we created to obtain the methond name of the executing function.
26+
*/
27+
public class TestName implements BeforeEachCallback {
28+
29+
private volatile String name;
30+
31+
@Override
32+
public void beforeEach(ExtensionContext extensionContext) throws Exception {
33+
name = extensionContext.getTestMethod().get().getName();
34+
}
35+
36+
public String getMethodName() {
37+
return this.name;
38+
}
39+
}

ratis-server/src/test/java/org/apache/ratis/statemachine/RaftSnapshotBaseTest.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,10 @@
5050
import org.apache.ratis.util.JavaUtils;
5151
import org.apache.ratis.util.LifeCycle;
5252
import org.apache.ratis.util.Slf4jUtils;
53-
import org.junit.After;
5453
import org.junit.Assert;
55-
import org.junit.Before;
5654
import org.junit.Test;
55+
import org.junit.jupiter.api.AfterEach;
56+
import org.junit.jupiter.api.BeforeEach;
5757
import org.slf4j.Logger;
5858
import org.slf4j.LoggerFactory;
5959

@@ -124,7 +124,7 @@ public static void assertLogContent(RaftServer.Division server, boolean isLeader
124124

125125
public abstract MiniRaftCluster.Factory<?> getFactory();
126126

127-
@Before
127+
@BeforeEach
128128
public void setup() throws IOException {
129129
final RaftProperties prop = new RaftProperties();
130130
prop.setClass(MiniRaftCluster.STATEMACHINE_CLASS_KEY,
@@ -136,7 +136,7 @@ public void setup() throws IOException {
136136
cluster.start();
137137
}
138138

139-
@After
139+
@AfterEach
140140
public void tearDown() {
141141
if (cluster != null) {
142142
cluster.shutdown();

0 commit comments

Comments
 (0)