Skip to content

Commit b5cbe87

Browse files
committed
Migrate all Java unit tests from JUnit 4 to JUnit 5
Replace @ClassRule/@rule with @RegisterExtension, remove JUnitSuite base class, migrate Assert to Assertions, convert @test(expected=...) to assertThrows(). Add thread-safe synchronized lifecycle methods to PekkoJUnit5ActorSystemResource. 204 files changed across actor-tests, actor-typed-tests, stream-tests, docs, coordination, cluster-tools, persistence-query, and actor-testkit-typed modules.
1 parent 73668de commit b5cbe87

205 files changed

Lines changed: 2012 additions & 1658 deletions

File tree

Some content is hidden

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

actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/AsyncTestingExampleTest.java

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -20,28 +20,32 @@
2020
import org.apache.pekko.actor.typed.javadsl.Behaviors;
2121
// #test-header
2222
import org.apache.pekko.actor.testkit.typed.javadsl.ActorTestKit;
23+
import org.apache.pekko.actor.testkit.typed.javadsl.JUnit5TestKitBuilder;
24+
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJUnit5Extension;
25+
import org.apache.pekko.actor.testkit.typed.javadsl.LogCapturingExtension;
2326

2427
// #test-header
2528
import org.apache.pekko.actor.testkit.typed.javadsl.TestProbe;
26-
import org.junit.AfterClass;
27-
import org.junit.Test;
28-
import org.scalatestplus.junit.JUnitSuite;
29+
import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
30+
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.extension.ExtendWith;
2932

3033
import java.time.Duration;
3134
import java.util.concurrent.CompletionStage;
3235
import java.util.stream.IntStream;
3336

3437
import java.util.Objects;
3538

36-
import static org.junit.Assert.assertEquals;
39+
import static org.junit.jupiter.api.Assertions.assertEquals;
3740

3841
// #test-header
42+
@ExtendWith({TestKitJUnit5Extension.class, LogCapturingExtension.class})
3943
public class AsyncTestingExampleTest
4044
// #test-header
41-
extends JUnitSuite
45+
4246
// #test-header
4347
{
44-
static final ActorTestKit testKit = ActorTestKit.create();
48+
@JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build();
4549
// #test-header
4650

4751
// #under-test
@@ -128,10 +132,7 @@ private CompletionStage<Integer> publish(int i) {
128132
// #under-test-2
129133

130134
// #test-shutdown
131-
@AfterClass
132-
public static void cleanup() {
133-
testKit.shutdownTestKit();
134-
}
135+
// Test cleanup is automatically handled by JUnit 5 extension
135136
// #test-shutdown
136137

137138
@Test
@@ -202,7 +203,7 @@ public void testObserveMockedBehavior() {
202203

203204
@Test
204205
public void systemNameShouldComeFromTestClass() {
205-
assertEquals(testKit.system().name(), "AsyncTestingExampleTest");
206+
assertEquals("AsyncTestingExampleTest", testKit.system().name());
206207
}
207208
// #test-header
208209
}

actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/JunitIntegrationExampleTest.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -15,24 +15,23 @@
1515

1616
import static jdocs.org.apache.pekko.actor.testkit.typed.javadsl.AsyncTestingExampleTest.Echo;
1717

18-
import org.apache.pekko.actor.testkit.typed.javadsl.LogCapturing;
19-
import org.junit.Rule;
18+
import org.apache.pekko.actor.testkit.typed.javadsl.LogCapturingExtension;
2019

2120
// #junit-integration
22-
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJunitResource;
21+
import org.apache.pekko.actor.testkit.typed.javadsl.ActorTestKit;
22+
import org.apache.pekko.actor.testkit.typed.javadsl.JUnit5TestKitBuilder;
23+
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJUnit5Extension;
2324
import org.apache.pekko.actor.testkit.typed.javadsl.TestProbe;
25+
import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
2426
import org.apache.pekko.actor.typed.ActorRef;
25-
import org.junit.ClassRule;
26-
import org.junit.Test;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
2729

30+
@ExtendWith(TestKitJUnit5Extension.class)
31+
@ExtendWith(LogCapturingExtension.class)
2832
public class JunitIntegrationExampleTest {
2933

30-
@ClassRule public static final TestKitJunitResource testKit = new TestKitJunitResource();
31-
32-
// #junit-integration
33-
// this is shown in LogCapturingExampleTest
34-
@Rule public final LogCapturing logCapturing = new LogCapturing();
35-
// #junit-integration
34+
@JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build();
3635

3736
@Test
3837
public void testSomething() {

actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/LogCapturingExampleTest.java

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,19 +16,21 @@
1616
import static jdocs.org.apache.pekko.actor.testkit.typed.javadsl.AsyncTestingExampleTest.*;
1717

1818
// #log-capturing
19-
import org.apache.pekko.actor.testkit.typed.javadsl.LogCapturing;
20-
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJunitResource;
19+
import org.apache.pekko.actor.testkit.typed.javadsl.LogCapturingExtension;
20+
import org.apache.pekko.actor.testkit.typed.javadsl.ActorTestKit;
21+
import org.apache.pekko.actor.testkit.typed.javadsl.JUnit5TestKitBuilder;
22+
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJUnit5Extension;
2123
import org.apache.pekko.actor.testkit.typed.javadsl.TestProbe;
24+
import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
2225
import org.apache.pekko.actor.typed.ActorRef;
23-
import org.junit.ClassRule;
24-
import org.junit.Rule;
25-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.extension.ExtendWith;
2628

29+
@ExtendWith(TestKitJUnit5Extension.class)
30+
@ExtendWith(LogCapturingExtension.class)
2731
public class LogCapturingExampleTest {
2832

29-
@ClassRule public static final TestKitJunitResource testKit = new TestKitJunitResource();
30-
31-
@Rule public final LogCapturing logCapturing = new LogCapturing();
33+
@JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build();
3234

3335
@Test
3436
public void testSomething() {

actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/ManualTimerExampleTest.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,23 +16,21 @@
1616
// #manual-scheduling-simple
1717

1818
import java.time.Duration;
19-
import org.apache.pekko.actor.testkit.typed.javadsl.LogCapturing;
19+
import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
20+
import org.apache.pekko.actor.testkit.typed.javadsl.ActorTestKit;
21+
import org.apache.pekko.actor.testkit.typed.javadsl.LogCapturingExtension;
2022
import org.apache.pekko.actor.testkit.typed.javadsl.ManualTime;
21-
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJunitResource;
23+
import org.apache.pekko.actor.testkit.typed.javadsl.TestKitJUnit5Extension;
2224
import org.apache.pekko.actor.testkit.typed.javadsl.TestProbe;
2325
import org.apache.pekko.actor.typed.Behavior;
2426
import org.apache.pekko.actor.typed.javadsl.Behaviors;
25-
import org.junit.ClassRule;
26-
import org.junit.Rule;
27-
import org.junit.Test;
28-
import org.scalatestplus.junit.JUnitSuite;
27+
import org.junit.jupiter.api.Test;
28+
import org.junit.jupiter.api.extension.ExtendWith;
2929

30-
public class ManualTimerExampleTest extends JUnitSuite {
30+
@ExtendWith({TestKitJUnit5Extension.class, LogCapturingExtension.class})
31+
public class ManualTimerExampleTest {
3132

32-
@ClassRule
33-
public static final TestKitJunitResource testKit = new TestKitJunitResource(ManualTime.config());
34-
35-
@Rule public final LogCapturing logCapturing = new LogCapturing();
33+
@JUnit5TestKit public ActorTestKit testKit = ActorTestKit.create(ManualTime.config());
3634

3735
private final ManualTime manualTime = ManualTime.get(testKit.system());
3836

actor-testkit-typed/src/test/java/jdocs/org/apache/pekko/actor/testkit/typed/javadsl/SyncTestingExampleTest.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
package jdocs.org.apache.pekko.actor.testkit.typed.javadsl;
1515

1616
// #imports
17-
import static org.junit.Assert.assertEquals;
18-
import static org.junit.Assert.assertTrue;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
18+
import static org.junit.jupiter.api.Assertions.assertTrue;
1919

2020
import com.typesafe.config.Config;
2121
import java.time.Duration;
@@ -28,13 +28,12 @@
2828
import org.apache.pekko.actor.testkit.typed.javadsl.TestInbox;
2929
import org.apache.pekko.actor.typed.*;
3030
import org.apache.pekko.actor.typed.javadsl.*;
31-
import org.junit.Test;
32-
import org.scalatestplus.junit.JUnitSuite;
31+
import org.junit.jupiter.api.Test;
3332
import org.slf4j.event.Level;
3433

3534
// #imports
3635

37-
public class SyncTestingExampleTest extends JUnitSuite {
36+
public class SyncTestingExampleTest {
3837

3938
// #child
4039
public static class Child {
@@ -315,20 +314,20 @@ public void testSupportContextualAsk() {
315314

316315
// Completing/timing-out the ask is processed synchronously
317316
List<CapturedLogEvent> allLogEntries = test.getAllLogEntries();
318-
assertEquals(allLogEntries.size(), 1);
317+
assertEquals(1, allLogEntries.size());
319318

320319
// The message, including the synthesized "replyTo", can be inspected from the effect
321320
assertEquals(question, effect.askMessage());
322321

323322
// The response adaptation can be tested as many times as you want without completing the ask
324323
Hello.Command response1 = effect.adaptResponse(new Hello.Answer("No. Who are you?"));
325-
assertEquals(((Hello.GotAnAnswer) response1).answer, "No. Who are you?");
324+
assertEquals("No. Who are you?", ((Hello.GotAnAnswer) response1).answer);
326325

327326
// ... as can the message sent on a timeout
328327
assertTrue(effect.adaptTimeout() instanceof Hello.NoAnswerFrom);
329328

330329
// The response timeout is captured
331-
assertEquals(effect.responseTimeout().toSeconds(), 10L);
330+
assertEquals(10L, effect.responseTimeout().toSeconds());
332331
// #test-contextual-ask
333332
}
334333

actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/ActorTestKitJUnit5Test.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,11 @@
2222
import org.junit.jupiter.api.DisplayName;
2323
import org.junit.jupiter.api.Test;
2424
import org.junit.jupiter.api.extension.ExtendWith;
25-
import org.scalatestplus.junit.JUnitSuite;
2625

2726
@DisplayName("ActorTestKitTestJUnit5")
2827
@ExtendWith(TestKitJUnit5Extension.class)
2928
@ExtendWith(LogCapturingExtension.class)
30-
class ActorTestKitJUnit5Test extends JUnitSuite {
29+
class ActorTestKitJUnit5Test {
3130

3231
@JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build();
3332

actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/ActorTestKitTest.java

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,21 @@
1414
package org.apache.pekko.actor.testkit.typed.javadsl;
1515

1616
import static org.apache.pekko.Done.done;
17-
import static org.junit.Assert.assertEquals;
17+
import static org.junit.jupiter.api.Assertions.assertEquals;
1818

1919
import java.util.HashMap;
2020
import java.util.concurrent.CompletableFuture;
2121
import java.util.concurrent.TimeUnit;
2222
import org.apache.pekko.Done;
23+
import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
2324
import org.apache.pekko.actor.typed.javadsl.Behaviors;
24-
import org.junit.ClassRule;
25-
import org.junit.Rule;
26-
import org.junit.Test;
27-
import org.scalatestplus.junit.JUnitSuite;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.extension.ExtendWith;
2827

29-
public class ActorTestKitTest extends JUnitSuite {
28+
@ExtendWith({TestKitJUnit5Extension.class, LogCapturingExtension.class})
29+
public class ActorTestKitTest {
3030

31-
@ClassRule public static TestKitJunitResource testKit = new TestKitJunitResource();
32-
33-
@Rule public final LogCapturing logCapturing = new LogCapturing();
31+
@JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build();
3432

3533
@Test
3634
public void systemNameShouldComeFromTestClassViaJunitResource() {

actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/BehaviorTestKitTest.java

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
package org.apache.pekko.actor.testkit.typed.javadsl;
1515

16-
import static org.junit.Assert.*;
16+
import static org.junit.jupiter.api.Assertions.*;
1717

1818
import java.time.Duration;
1919
import java.util.Arrays;
@@ -27,12 +27,11 @@
2727
import org.apache.pekko.actor.typed.Behavior;
2828
import org.apache.pekko.actor.typed.Props;
2929
import org.apache.pekko.actor.typed.javadsl.Behaviors;
30-
import org.junit.Ignore;
31-
import org.junit.Test;
32-
import org.scalatestplus.junit.JUnitSuite;
30+
import org.junit.jupiter.api.Disabled;
31+
import org.junit.jupiter.api.Test;
3332
import org.slf4j.event.Level;
3433

35-
public class BehaviorTestKitTest extends JUnitSuite {
34+
public class BehaviorTestKitTest {
3635

3736
public interface Command {}
3837

@@ -279,7 +278,7 @@ public void allowAssertionsOnEffectType() {
279278
BehaviorTestKit<Command> test = BehaviorTestKit.create(behavior);
280279
test.run(new SpawnChildren(1));
281280
Effect.Spawned spawned = test.expectEffectClass(Effect.Spawned.class);
282-
assertEquals(spawned.childName(), "child0");
281+
assertEquals("child0", spawned.childName());
283282
}
284283

285284
@Test
@@ -323,7 +322,7 @@ public void returnEffectsThatHaveTakenPlace() {
323322
}
324323

325324
@Test
326-
@Ignore("Not supported for Java API")
325+
@Disabled("Not supported for Java API")
327326
public void allowAssertionsUsingPartialFunctions() {}
328327

329328
@Test

actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/LoggingTestKitTest.java

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,24 +13,22 @@
1313

1414
package org.apache.pekko.actor.testkit.typed.javadsl;
1515

16-
import static org.junit.Assert.assertFalse;
17-
import static org.junit.Assert.assertTrue;
16+
import static org.junit.jupiter.api.Assertions.assertFalse;
17+
import static org.junit.jupiter.api.Assertions.assertTrue;
1818

1919
import java.util.Collections;
2020
import java.util.Optional;
2121
import org.apache.pekko.actor.testkit.typed.LoggingEvent;
2222
import org.apache.pekko.actor.testkit.typed.TestException;
23-
import org.junit.ClassRule;
24-
import org.junit.Rule;
25-
import org.junit.Test;
26-
import org.scalatestplus.junit.JUnitSuite;
23+
import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.ExtendWith;
2726
import org.slf4j.event.Level;
2827

29-
public class LoggingTestKitTest extends JUnitSuite {
28+
@ExtendWith({TestKitJUnit5Extension.class, LogCapturingExtension.class})
29+
public class LoggingTestKitTest {
3030

31-
@ClassRule public static TestKitJunitResource testKit = new TestKitJunitResource();
32-
33-
@Rule public final LogCapturing logCapturing = new LogCapturing();
31+
@JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build();
3432

3533
private LoggingEvent errorNoCause() {
3634
return LoggingEvent.create(

actor-testkit-typed/src/test/java/org/apache/pekko/actor/testkit/typed/javadsl/TestProbeTest.java

Lines changed: 22 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,21 @@
1313

1414
package org.apache.pekko.actor.testkit.typed.javadsl;
1515

16-
import static org.junit.Assert.*;
16+
import static org.junit.jupiter.api.Assertions.*;
1717

1818
import java.time.Duration;
1919
import java.util.Arrays;
2020
import java.util.List;
21+
import org.apache.pekko.actor.testkit.typed.annotations.JUnit5TestKit;
2122
import org.apache.pekko.actor.testkit.typed.scaladsl.TestProbeSpec;
2223
import org.apache.pekko.actor.testkit.typed.scaladsl.TestProbeSpec.*;
23-
import org.junit.ClassRule;
24-
import org.junit.Rule;
25-
import org.junit.Test;
26-
import org.scalatestplus.junit.JUnitSuite;
24+
import org.junit.jupiter.api.Test;
25+
import org.junit.jupiter.api.extension.ExtendWith;
2726

28-
public class TestProbeTest extends JUnitSuite {
27+
@ExtendWith({TestKitJUnit5Extension.class, LogCapturingExtension.class})
28+
public class TestProbeTest {
2929

30-
@ClassRule public static TestKitJunitResource testKit = new TestKitJunitResource();
31-
32-
@Rule public final LogCapturing logCapturing = new LogCapturing();
30+
@JUnit5TestKit public ActorTestKit testKit = new JUnit5TestKitBuilder().build();
3331

3432
@Test
3533
public void testReceiveMessage() {
@@ -61,10 +59,14 @@ public void testReceiveMessageMaxDuration() {
6159
probe.expectNoMessage();
6260
}
6361

64-
@Test(expected = AssertionError.class)
62+
@Test
6563
public void testReceiveMessageFailOnTimeout() {
66-
TestProbe<EventT> probe = TestProbe.create(testKit.system());
67-
probe.receiveMessage(Duration.ofMillis(100));
64+
org.junit.jupiter.api.Assertions.assertThrows(
65+
AssertionError.class,
66+
() -> {
67+
TestProbe<EventT> probe = TestProbe.create(testKit.system());
68+
probe.receiveMessage(Duration.ofMillis(100));
69+
});
6870
}
6971

7072
@Test
@@ -92,12 +94,16 @@ public void testAwaitAssert() {
9294
assertEquals("some result", awaitAssertResult);
9395
}
9496

95-
@Test(expected = Exception.class)
97+
@Test
9698
public void testAwaitAssertThrowingCheckedException() {
97-
TestProbe<String> probe = TestProbe.create(testKit.system());
98-
probe.awaitAssert(
99+
org.junit.jupiter.api.Assertions.assertThrows(
100+
Exception.class,
99101
() -> {
100-
throw new Exception("checked exception");
102+
TestProbe<String> probe = TestProbe.create(testKit.system());
103+
probe.awaitAssert(
104+
() -> {
105+
throw new Exception("checked exception");
106+
});
101107
});
102108
}
103109

0 commit comments

Comments
 (0)