Skip to content

Commit 7989344

Browse files
committed
[SPARK-53872] Revisit JUnit assert usage in test cases
### What changes were proposed in this pull request? This PR aims to revisit `JUnit` assertion usage in test cases after upgrading to `JUnit 6`. - apache#383 ### Why are the changes needed? There are three cases: 1. Fix wrong usage of `assertEquals`: The first parameter is the expected value. ```java - assertEquals(previousGeneration2, 1L); + assertEquals(1L, previousGeneration2); ``` 2. `assertNull` is better than `assertEquals(null, *)`. ```java - assertEquals(null, spec1.runtimeVersions.jdkVersion); - assertEquals(null, spec1.runtimeVersions.scalaVersion); - assertEquals(null, spec1.runtimeVersions.sparkVersion); + assertNull(spec1.runtimeVersions.jdkVersion); + assertNull(spec1.runtimeVersions.scalaVersion); + assertNull(spec1.runtimeVersions.sparkVersion); ``` 3. `assertInstanceOf` is more intuitive than `assertTrue(... instanceof ...)`. ```java - assertTrue(constructorArgs.get(conf).get(0) instanceof SparkConf); + assertInstanceOf(SparkConf.class, constructorArgs.get(conf).get(0)); ``` ### Does this PR introduce _any_ user-facing change? No. This is a test case fix and improvement. ### How was this patch tested? Pass the CIs. ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#384 from dongjoon-hyun/SPARK-53872. Authored-by: Dongjoon Hyun <[email protected]> Signed-off-by: Dongjoon Hyun <[email protected]>
1 parent 0408157 commit 7989344

File tree

7 files changed

+20
-19
lines changed

7 files changed

+20
-19
lines changed

spark-operator-api/src/test/java/org/apache/spark/k8s/operator/spec/ClusterSpecTest.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
package org.apache.spark.k8s.operator.spec;
2121

2222
import static org.junit.jupiter.api.Assertions.assertEquals;
23+
import static org.junit.jupiter.api.Assertions.assertNull;
2324

2425
import org.junit.jupiter.api.Test;
2526

@@ -34,9 +35,9 @@ void testBuilder() {
3435
@Test
3536
void testInitSpecWithDefaults() {
3637
ClusterSpec spec1 = new ClusterSpec();
37-
assertEquals(null, spec1.runtimeVersions.jdkVersion);
38-
assertEquals(null, spec1.runtimeVersions.scalaVersion);
39-
assertEquals(null, spec1.runtimeVersions.sparkVersion);
38+
assertNull(spec1.runtimeVersions.jdkVersion);
39+
assertNull(spec1.runtimeVersions.scalaVersion);
40+
assertNull(spec1.runtimeVersions.sparkVersion);
4041
assertEquals(0, spec1.clusterTolerations.instanceConfig.initWorkers);
4142
assertEquals(0, spec1.clusterTolerations.instanceConfig.minWorkers);
4243
assertEquals(0, spec1.clusterTolerations.instanceConfig.maxWorkers);

spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/MetricsSystemTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ void testMetricsSystemWithCustomizedSink() {
6363
Sink mockSink = mockSinkOptional.get();
6464
metricsSystem.stop();
6565
MockSink sink = (MockSink) mockSink;
66-
assertEquals(sink.getPollPeriod(), 10);
67-
assertEquals(sink.getTimeUnit(), TimeUnit.SECONDS);
66+
assertEquals(10, sink.getPollPeriod());
67+
assertEquals(TimeUnit.SECONDS, sink.getTimeUnit());
6868
}
6969

7070
@Test

spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/healthcheck/SentinelManagerTest.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -109,7 +109,7 @@ void testHandleSentinelResourceReconciliation() throws InterruptedException {
109109
kubernetesClient.resources(SparkApplication.class).inNamespace(DEFAULT).list();
110110
SparkApplication sparkApplication = crList.getItems().get(0);
111111
Long generation = sparkApplication.getMetadata().getGeneration();
112-
assertEquals(generation, 1L);
112+
assertEquals(1L, generation);
113113

114114
// Spark Reconciler Handle Sentinel Resources at the first time
115115
var sentinelManager = new SentinelManager<SparkApplication>();
@@ -120,12 +120,12 @@ void testHandleSentinelResourceReconciliation() throws InterruptedException {
120120
Map<String, String> sparkConf2 = new HashMap<>(sparkApplication2.getSpec().getSparkConf());
121121
long generation2 = sparkApplication2.getMetadata().getGeneration();
122122

123-
assertEquals(sparkConf2.get(Constants.SENTINEL_RESOURCE_DUMMY_FIELD), "1");
124-
assertEquals(generation2, 2L);
123+
assertEquals("1", sparkConf2.get(Constants.SENTINEL_RESOURCE_DUMMY_FIELD));
124+
assertEquals(2L, generation2);
125125
var state2 = sentinelManager.getSentinelResources().get(ResourceID.fromResource(mockApp));
126126
long previousGeneration2 = state2.previousGeneration;
127127
assertTrue(sentinelManager.allSentinelsAreHealthy());
128-
assertEquals(previousGeneration2, 1L);
128+
assertEquals(1L, previousGeneration2);
129129

130130
Thread.sleep(Duration.ofSeconds(SENTINEL_RESOURCE_RECONCILIATION_DELAY_SECONDS * 2).toMillis());
131131
List<SparkApplication> crList3 =
@@ -194,8 +194,8 @@ void sentinelManagerShouldReportHealthyWhenWatchedNamespaceIsReduced()
194194
sentinelManager.handleSentinelResourceReconciliation(sparkApplication1, kubernetesClient);
195195
sentinelManager.handleSentinelResourceReconciliation(sparkApplication2, kubernetesClient);
196196
assertEquals(
197-
sentinelManager.getSentinelResources().size(),
198197
2,
198+
sentinelManager.getSentinelResources().size(),
199199
"Sentinel Manager should watch on resources in two namespaces");
200200
assertTrue(
201201
sentinelManager.allSentinelsAreHealthy(), "Sentinel Manager should report healthy");
@@ -206,8 +206,8 @@ void sentinelManagerShouldReportHealthyWhenWatchedNamespaceIsReduced()
206206
"Sentinel Manager should report healthy after one namespace is "
207207
+ "removed from the watch");
208208
assertEquals(
209-
sentinelManager.getSentinelResources().size(),
210209
1,
210+
sentinelManager.getSentinelResources().size(),
211211
"Sentinel Manager should only watch on one namespace");
212212
}
213213
}

spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/source/KubernetesMetricsInterceptorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,9 +94,9 @@ void testMetricsEnabled() {
9494
.forEach(
9595
name -> {
9696
Meter metric = (Meter) metrics2.get(name);
97-
Assertions.assertEquals(metric.getCount(), 1);
97+
Assertions.assertEquals(1, metric.getCount());
9898
});
99-
Assertions.assertEquals(((Meter) metrics2.get("http.request")).getCount(), 2);
99+
Assertions.assertEquals(2, ((Meter) metrics2.get("http.request")).getCount());
100100
client.resource(sparkApplication).delete();
101101
}
102102
}

spark-operator/src/test/java/org/apache/spark/k8s/operator/metrics/source/OperatorJosdkMetricsTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ void testTimeControllerExecution() throws Exception {
7676
try {
7777
operatorMetrics.timeControllerExecution(failedExecution);
7878
} catch (Exception e) {
79-
assertEquals(e.getMessage(), "Foo exception");
79+
assertEquals("Foo exception", e.getMessage());
8080
assertEquals(8, metrics.size());
8181
assertTrue(metrics.containsKey("sparkapplication.test-controller.reconcile.failure"));
8282
assertTrue(

spark-operator/src/test/java/org/apache/spark/k8s/operator/probe/ProbeServiceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -117,14 +117,14 @@ private void hitHealthyEndpoint() throws IOException, MalformedURLException {
117117
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
118118
connection.setConnectTimeout(100000);
119119
connection.connect();
120-
assertEquals(connection.getResponseCode(), HTTP_OK, "Health Probe should return HTTP_OK");
120+
assertEquals(HTTP_OK, connection.getResponseCode(), "Health Probe should return HTTP_OK");
121121
}
122122

123123
private void hitStartedUpEndpoint() throws IOException, MalformedURLException {
124124
URL u = new URL("http://localhost:" + OPERATOR_PROBE_PORT.getValue() + READYZ);
125125
HttpURLConnection connection = (HttpURLConnection) u.openConnection();
126126
connection.setConnectTimeout(100000);
127127
connection.connect();
128-
assertEquals(connection.getResponseCode(), HTTP_OK, "operators are not ready");
128+
assertEquals(HTTP_OK, connection.getResponseCode(), "operators are not ready");
129129
}
130130
}

spark-submission-worker/src/test/java/org/apache/spark/k8s/operator/SparkAppSubmissionWorkerTest.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ void buildDriverConfShouldApplySpecAndPropertiesOverride() {
7979
assertEquals(6, constructorArgs.get(conf).size());
8080

8181
// validate SparkConf with override
82-
assertTrue(constructorArgs.get(conf).get(0) instanceof SparkConf);
82+
assertInstanceOf(SparkConf.class, constructorArgs.get(conf).get(0));
8383
SparkConf createdConf = (SparkConf) constructorArgs.get(conf).get(0);
8484
assertEquals("bar", createdConf.get("foo"));
8585
assertEquals("5", createdConf.get("spark.executor.instances"));
@@ -90,13 +90,13 @@ void buildDriverConfShouldApplySpecAndPropertiesOverride() {
9090
"namespace from CR takes highest precedence");
9191

9292
// validate main resources
93-
assertTrue(constructorArgs.get(conf).get(2) instanceof JavaMainAppResource);
93+
assertInstanceOf(JavaMainAppResource.class, constructorArgs.get(conf).get(2));
9494
JavaMainAppResource mainResource = (JavaMainAppResource) constructorArgs.get(conf).get(2);
9595
assertTrue(mainResource.primaryResource().isEmpty());
9696

9797
assertEquals("foo-class", constructorArgs.get(conf).get(3));
9898

99-
assertTrue(constructorArgs.get(conf).get(4) instanceof String[]);
99+
assertInstanceOf(String[].class, constructorArgs.get(conf).get(4));
100100
String[] capturedArgs = (String[]) constructorArgs.get(conf).get(4);
101101
assertEquals(2, capturedArgs.length);
102102
assertEquals("a", capturedArgs[0]);

0 commit comments

Comments
 (0)