Skip to content

Commit df896e8

Browse files
authored
[Issue-664] Migrate from JUnit4 to JUnit5 (#689)
Signed-off-by: Fan, Yang <[email protected]>
1 parent 40e3bde commit df896e8

37 files changed

+596
-575
lines changed

build.gradle

+3
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,8 @@ dependencies {
148148
testImplementation group: 'org.apache.flink', name: 'flink-avro', version: flinkVersion
149149
testImplementation group: 'org.hamcrest', name: 'hamcrest', version: hamcrestVersion
150150
testImplementation group: 'org.testcontainers', name: 'testcontainers', version: testcontainersVersion
151+
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter', version: junit5Version
152+
testImplementation group: 'org.assertj', name: 'assertj-core', version: assertjVersion
151153
}
152154

153155
javadoc {
@@ -171,4 +173,5 @@ def getProjectVersion() {
171173
// for e.g., to use standalone Pravega for running the system test "./gradlew clean build -Dpravega.uri=tcp://localhost:9090"
172174
test {
173175
systemProperties = System.properties
176+
useJUnitPlatform()
174177
}

checkstyle/import-control.xml

+1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
<allow pkg="io.pravega"/>
1717
<allow pkg="org.apache"/>
1818
<allow pkg="edu.umd.cs.findbugs"/>
19+
<allow pkg="org.assertj"/>
1920

2021
<!-- test dependencies -->
2122
<allow pkg="org.mockito"/>

gradle.properties

+2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#
1616

1717
# 3rd party Versions.
18+
assertjVersion=3.23.1
1819
checkstyleToolVersion=7.1
1920
flinkVersion=1.15.0
2021
flinkScalaVersion=2.12
@@ -23,6 +24,7 @@ twitterMvnRepoVersion=4.3.4-TWTTR
2324
shadowGradlePlugin=7.0.0
2425
slf4jApiVersion=1.7.25
2526
junitVersion=4.12
27+
junit5Version=5.8.1
2628
mockitoVersion=3.3.3
2729
gradleGitPluginVersion=4.1.0
2830
spotbugsVersion=4.0.6

src/test/java/io/pravega/connectors/flink/EventTimeOrderingFunctionTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -23,9 +23,9 @@
2323
import org.apache.flink.streaming.util.KeyedOneInputStreamOperatorTestHarness;
2424
import org.apache.flink.streaming.util.ProcessFunctionTestHarnesses;
2525
import org.apache.flink.streaming.util.TestHarnessUtil;
26-
import org.junit.After;
27-
import org.junit.Before;
28-
import org.junit.Test;
26+
import org.junit.jupiter.api.AfterEach;
27+
import org.junit.jupiter.api.BeforeEach;
28+
import org.junit.jupiter.api.Test;
2929

3030
import java.util.Queue;
3131
import java.util.concurrent.ConcurrentLinkedQueue;
@@ -40,15 +40,15 @@ public class EventTimeOrderingFunctionTest {
4040
private EventTimeOrderingFunction<Tuple2<String, Long>> function;
4141
private KeyedOneInputStreamOperatorTestHarness<String, Tuple2<String, Long>, Tuple2<String, Long>> testHarness;
4242

43-
@Before
43+
@BeforeEach
4444
public void before() throws Exception {
4545
function = new EventTimeOrderingFunction<>(TypeInformation.of(new TypeHint<Tuple2<String, Long>>() {
4646
}));
4747
testHarness = ProcessFunctionTestHarnesses.forKeyedProcessFunction(function, in -> in.f0, TypeInformation.of(String.class));
4848
testHarness.open();
4949
}
5050

51-
@After
51+
@AfterEach
5252
public void after() throws Exception {
5353
testHarness.close();
5454
function.close();

src/test/java/io/pravega/connectors/flink/FlinkPravegaInputFormatITCase.java

+13-16
Original file line numberDiff line numberDiff line change
@@ -29,31 +29,28 @@
2929
import org.apache.flink.api.java.DataSet;
3030
import org.apache.flink.api.java.ExecutionEnvironment;
3131
import org.apache.flink.test.util.AbstractTestBase;
32-
import org.junit.AfterClass;
33-
import org.junit.Assert;
34-
import org.junit.BeforeClass;
35-
import org.junit.Rule;
36-
import org.junit.Test;
37-
import org.junit.rules.Timeout;
32+
import org.junit.jupiter.api.AfterAll;
33+
import org.junit.jupiter.api.BeforeAll;
34+
import org.junit.jupiter.api.Test;
35+
import org.junit.jupiter.api.Timeout;
3836

3937
import java.util.HashSet;
4038
import java.util.List;
4139
import java.util.Set;
42-
import java.util.concurrent.TimeUnit;
4340

41+
import static org.assertj.core.api.Assertions.assertThat;
42+
43+
@Timeout(value = 120)
4444
public class FlinkPravegaInputFormatITCase extends AbstractTestBase {
4545

4646
private static final PravegaTestEnvironment PRAVEGA = new PravegaTestEnvironment(PravegaRuntime.container());
4747

48-
@Rule
49-
public final Timeout globalTimeout = new Timeout(120, TimeUnit.SECONDS);
50-
51-
@BeforeClass
48+
@BeforeAll
5249
public static void setupPravega() throws Exception {
5350
PRAVEGA.startUp();
5451
}
5552

56-
@AfterClass
53+
@AfterAll
5754
public static void tearDownPravega() throws Exception {
5855
PRAVEGA.tearDown();
5956
}
@@ -124,10 +121,10 @@ public void testBatchInput() throws Exception {
124121
);
125122

126123
// verify that all events were read
127-
Assert.assertEquals(numElements1 + numElements2, integers.collect().size());
124+
assertThat(integers.collect().size()).isEqualTo(numElements1 + numElements2);
128125

129126
// this verifies that the input format allows multiple passes
130-
Assert.assertEquals(numElements1 + numElements2, integers.collect().size());
127+
assertThat(integers.collect().size()).isEqualTo(numElements1 + numElements2);
131128
}
132129
}
133130

@@ -173,8 +170,8 @@ public void testBatchInputWithFailure() throws Exception {
173170
).map(new FailOnceMapper(numElements / 2)).collect();
174171

175172
// verify that the job did fail, and all events were still read
176-
Assert.assertTrue(FailOnceMapper.hasFailed());
177-
Assert.assertEquals(numElements, integers.size());
173+
assertThat(FailOnceMapper.hasFailed()).isTrue();
174+
assertThat(integers.size()).isEqualTo(numElements);
178175

179176
FailOnceMapper.reset();
180177
}

src/test/java/io/pravega/connectors/flink/FlinkPravegaInputFormatTest.java

+19-14
Original file line numberDiff line numberDiff line change
@@ -26,12 +26,13 @@
2626
import org.apache.flink.api.common.io.DefaultInputSplitAssigner;
2727
import org.apache.flink.api.common.io.InputFormat;
2828
import org.apache.flink.api.common.serialization.DeserializationSchema;
29-
import org.junit.Test;
29+
import org.junit.jupiter.api.Test;
3030

3131
import java.net.URI;
3232
import java.util.Iterator;
3333

34-
import static org.junit.Assert.fail;
34+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
35+
import static org.assertj.core.api.Assertions.fail;
3536
import static org.mockito.Matchers.any;
3637
import static org.mockito.Matchers.anyObject;
3738
import static org.mockito.Matchers.anyString;
@@ -78,27 +79,31 @@ public void testBuilderForSuccess() {
7879
/**
7980
* Testing the builder for missing configurations.
8081
*/
81-
@Test (expected = IllegalStateException.class)
82+
@Test
8283
public void testBuilderForMissingDeSerializationSchema() {
8384
doReturn(clientConfig).when(pravegaConfig).getClientConfig();
8485
doReturn(stream).when(pravegaConfig).resolve(anyString());
85-
FlinkPravegaInputFormat.<String>builder()
86-
.withPravegaConfig(pravegaConfig)
87-
.forStream(stream)
88-
.build();
86+
assertThatThrownBy(
87+
() -> FlinkPravegaInputFormat.<String>builder()
88+
.withPravegaConfig(pravegaConfig)
89+
.forStream(stream)
90+
.build())
91+
.isInstanceOf(IllegalStateException.class);
8992
}
9093

9194
/**
9295
* Testing the builder for missing configurations.
9396
*/
94-
@Test (expected = IllegalStateException.class)
97+
@Test
9598
public void testBuilderForMissingStream() {
9699
doReturn(clientConfig).when(pravegaConfig).getClientConfig();
97100
doReturn(stream).when(pravegaConfig).resolve(anyString());
98-
FlinkPravegaInputFormat.<String>builder()
99-
.withDeserializationSchema(deserializationSchema)
100-
.withPravegaConfig(pravegaConfig)
101-
.build();
101+
assertThatThrownBy(
102+
() -> FlinkPravegaInputFormat.<String>builder()
103+
.withDeserializationSchema(deserializationSchema)
104+
.withPravegaConfig(pravegaConfig)
105+
.build())
106+
.isInstanceOf(IllegalStateException.class);
102107
}
103108

104109
/**
@@ -113,7 +118,7 @@ public void testSchemaRegistryDeserialization() throws Exception {
113118
.forStream("stream")
114119
.withDeserializationSchemaFromRegistry("stream", Integer.class)
115120
.build();
116-
fail();
121+
fail(null);
117122
} catch (NullPointerException e) {
118123
// "missing default scope"
119124
}
@@ -125,7 +130,7 @@ public void testSchemaRegistryDeserialization() throws Exception {
125130
.forStream("stream")
126131
.withDeserializationSchemaFromRegistry("stream", Integer.class)
127132
.build();
128-
fail();
133+
fail(null);
129134
} catch (NullPointerException e) {
130135
// "missing Schema Registry URI"
131136
}

src/test/java/io/pravega/connectors/flink/FlinkPravegaOutputFormatITCase.java

+10-13
Original file line numberDiff line numberDiff line change
@@ -25,30 +25,27 @@
2525
import org.apache.flink.api.java.DataSet;
2626
import org.apache.flink.api.java.ExecutionEnvironment;
2727
import org.apache.flink.test.util.AbstractTestBase;
28-
import org.junit.AfterClass;
29-
import org.junit.Assert;
30-
import org.junit.BeforeClass;
31-
import org.junit.Rule;
32-
import org.junit.Test;
33-
import org.junit.rules.Timeout;
28+
import org.junit.jupiter.api.AfterAll;
29+
import org.junit.jupiter.api.BeforeAll;
30+
import org.junit.jupiter.api.Test;
31+
import org.junit.jupiter.api.Timeout;
3432

3533
import java.util.Arrays;
3634
import java.util.Collection;
37-
import java.util.concurrent.TimeUnit;
3835

36+
import static org.assertj.core.api.Assertions.assertThat;
37+
38+
@Timeout(value = 120)
3939
public class FlinkPravegaOutputFormatITCase extends AbstractTestBase {
4040

4141
private static final PravegaTestEnvironment PRAVEGA = new PravegaTestEnvironment(PravegaRuntime.container());
4242

43-
@Rule
44-
public final Timeout globalTimeout = new Timeout(120, TimeUnit.SECONDS);
45-
46-
@BeforeClass
43+
@BeforeAll
4744
public static void setupPravega() throws Exception {
4845
PRAVEGA.startUp();
4946
}
5047

51-
@AfterClass
48+
@AfterAll
5249
public static void tearDownPravega() throws Exception {
5350
PRAVEGA.tearDown();
5451
}
@@ -92,7 +89,7 @@ public void testPravegaOutputFormat() throws Exception {
9289
);
9390

9491
// verify that all events were read
95-
Assert.assertEquals(2, integers.collect().size());
92+
assertThat(integers.collect().size()).isEqualTo(2);
9693
}
9794

9895

src/test/java/io/pravega/connectors/flink/FlinkPravegaOutputFormatTest.java

+32-29
Original file line numberDiff line numberDiff line change
@@ -23,18 +23,17 @@
2323
import org.apache.commons.lang3.NotImplementedException;
2424
import org.apache.flink.api.common.io.OutputFormat;
2525
import org.apache.flink.api.common.serialization.SerializationSchema;
26-
import org.junit.Assert;
27-
import org.junit.Test;
26+
import org.junit.jupiter.api.Test;
2827
import org.mockito.Mockito;
2928

3029
import java.io.IOException;
3130
import java.net.URI;
3231
import java.util.concurrent.CompletableFuture;
3332
import java.util.concurrent.ExecutorService;
3433

35-
import static org.junit.Assert.assertEquals;
36-
import static org.junit.Assert.assertTrue;
37-
import static org.junit.Assert.fail;
34+
import static org.assertj.core.api.Assertions.assertThat;
35+
import static org.assertj.core.api.Assertions.assertThatThrownBy;
36+
import static org.assertj.core.api.Assertions.fail;
3837
import static org.mockito.Matchers.any;
3938
import static org.mockito.Matchers.anyObject;
4039
import static org.mockito.Matchers.anyString;
@@ -73,37 +72,41 @@ public void testBuilderForSuccess() {
7372
.withPravegaConfig(pravegaConfig)
7473
.forStream(stream)
7574
.build();
76-
assertEquals(stream.getScope(), outputFormat.getScope());
77-
assertEquals(stream.getStreamName(), outputFormat.getStream());
78-
assertEquals(serializationSchema, outputFormat.getSerializationSchema());
79-
assertEquals(eventRouter, outputFormat.getEventRouter());
75+
assertThat(outputFormat.getScope()).isEqualTo(stream.getScope());
76+
assertThat(outputFormat.getStream()).isEqualTo(stream.getStreamName());
77+
assertThat(outputFormat.getSerializationSchema()).isEqualTo(serializationSchema);
78+
assertThat(outputFormat.getEventRouter()).isEqualTo(eventRouter);
8079
}
8180

8281
/**
8382
* Testing the builder for right configurations.
8483
* Should fail since we don't pass {@link SerializationSchema}
8584
*/
86-
@Test(expected = NullPointerException.class)
85+
@Test
8786
public void testBuilderForFailure1() {
8887
PravegaConfig pravegaConfig = mock(PravegaConfig.class);
89-
FlinkPravegaOutputFormat.<String>builder()
90-
.withEventRouter(eventRouter)
91-
.withPravegaConfig(pravegaConfig)
92-
.forStream(stream)
93-
.build();
88+
assertThatThrownBy(
89+
() -> FlinkPravegaOutputFormat.<String>builder()
90+
.withEventRouter(eventRouter)
91+
.withPravegaConfig(pravegaConfig)
92+
.forStream(stream)
93+
.build())
94+
.isInstanceOf(NullPointerException.class);
9495
}
9596

9697
/**
9798
* Testing the builder for right configurations.
9899
* Should fail since we don't pass {@link Stream}
99100
*/
100-
@Test(expected = IllegalStateException.class)
101+
@Test
101102
public void testBuilderForFailure2() {
102-
FlinkPravegaOutputFormat.<String>builder()
103-
.withEventRouter(eventRouter)
104-
.withSerializationSchema(serializationSchema)
105-
.withPravegaConfig(pravegaConfig)
106-
.build();
103+
assertThatThrownBy(
104+
() -> FlinkPravegaOutputFormat.<String>builder()
105+
.withEventRouter(eventRouter)
106+
.withSerializationSchema(serializationSchema)
107+
.withPravegaConfig(pravegaConfig)
108+
.build())
109+
.isInstanceOf(IllegalStateException.class);
107110
}
108111

109112
/**
@@ -133,7 +136,7 @@ public void testLifecycleMethods() throws Exception {
133136

134137
// test writeRecord success
135138
spyFlinkPravegaOutputFormat.writeRecord("test-1");
136-
assertEquals(1, spyFlinkPravegaOutputFormat.getPendingWritesCount().get());
139+
assertThat(spyFlinkPravegaOutputFormat.getPendingWritesCount().get()).isEqualTo(1);
137140
writeFuture.complete(null);
138141

139142
// test writeRecord induce failure
@@ -144,17 +147,17 @@ public void testLifecycleMethods() throws Exception {
144147
try {
145148
spyFlinkPravegaOutputFormat.writeRecord("test-3");
146149
} catch (Exception e) {
147-
Assert.assertTrue(e instanceof IOException);
148-
assertTrue(spyFlinkPravegaOutputFormat.isErrorOccurred());
149-
assertEquals("test simulated", e.getCause().getMessage());
150-
assertEquals(0, spyFlinkPravegaOutputFormat.getPendingWritesCount().get());
150+
assertThat(e instanceof IOException).isTrue();
151+
assertThat(spyFlinkPravegaOutputFormat.isErrorOccurred()).isTrue();
152+
assertThat(e.getCause().getMessage()).isEqualTo("test simulated");
153+
assertThat(spyFlinkPravegaOutputFormat.getPendingWritesCount().get()).isEqualTo(0);
151154
}
152155

153156
// test close error
154157
try {
155158
spyFlinkPravegaOutputFormat.close();
156159
} catch (Exception e) {
157-
Assert.assertTrue(e instanceof IOException);
160+
assertThat(e instanceof IOException).isTrue();
158161
}
159162

160163
// test close
@@ -173,7 +176,7 @@ public void testSchemaRegistrySerialization() throws Exception {
173176
.forStream("stream")
174177
.withSerializationSchemaFromRegistry("stream", Integer.class)
175178
.build();
176-
fail();
179+
fail(null);
177180
} catch (NullPointerException e) {
178181
// "missing default scope"
179182
}
@@ -185,7 +188,7 @@ public void testSchemaRegistrySerialization() throws Exception {
185188
.forStream("stream")
186189
.withSerializationSchemaFromRegistry("stream", Integer.class)
187190
.build();
188-
fail();
191+
fail(null);
189192
} catch (NullPointerException e) {
190193
// "missing Schema Registry URI"
191194
}

0 commit comments

Comments
 (0)