Skip to content

Commit d8714c8

Browse files
Migrate TestNG tests to JUnit 5 (#474)
This PR migrates TestNG tests to JUnit 5. #### Changes: - Migrate tests to JUnit 5 - Merge operation tests - Merge operation serialization tests - Remove TestNG dependency and task.
1 parent 375cfbd commit d8714c8

26 files changed

+275
-921
lines changed

NOTICE.txt

-5
Original file line numberDiff line numberDiff line change
@@ -300,11 +300,6 @@ This product depends on Spring Framework, distributed by Pivotal Software, Inc:
300300
* License: licenses/LICENSE.spring.al20.txt (Apache License v2.0)
301301
* Homepage: https://spring.io/
302302

303-
This product depends on TestNG, distributed by Cédric Beust:
304-
305-
* License: licenses/LICENSE.testng.al20.txt (Apache License v2.0)
306-
* Homepage: http://testng.org/doc/
307-
308303
This product depends on Apache Thrift, distributed by Apache Software Foundation:
309304

310305
* License: licenses/LICENSE.thrift.al20.txt (Apache License v2.0)

common/build.gradle

-24
Original file line numberDiff line numberDiff line change
@@ -24,31 +24,7 @@ dependencies {
2424

2525
// JSON-path
2626
compile 'com.jayway.jsonpath:json-path'
27-
28-
// TestNG (for JSON patch tests)
29-
testCompile 'org.testng:testng'
30-
}
31-
32-
// Run the JSON patch test cases.
33-
task testNg(type: Test,
34-
group: 'Verification',
35-
description: 'Runs the TestNG unit tests.',
36-
dependsOn: tasks.shadedTestClasses) {
37-
38-
useTestNG()
39-
include '/com/linecorp/centraldogma/internal/jsonpatch/**'
40-
41-
scanForTestClasses = false
42-
testClassesDirs = tasks.shadedTest.testClassesDirs
43-
classpath = testClassesDirs
44-
45-
// Set the class path as late as possible so that the 'shadedTest' task has the correct classpath.
46-
doFirst {
47-
classpath += project.files(configurations.shadedTestRuntime.resolve())
48-
}
4927
}
50-
tasks.shadedTest.finalizedBy tasks.testNg
51-
tasks.check.dependsOn tasks.testNg
5228

5329
if (tasks.findByName('trimShadedJar')) {
5430
tasks.trimShadedJar.configure {

common/src/test/java/com/linecorp/centraldogma/internal/jsonpatch/AddOperationSerializationTest.java

-43
This file was deleted.

common/src/test/java/com/linecorp/centraldogma/internal/jsonpatch/AddOperationTest.java

-43
This file was deleted.

common/src/test/java/com/linecorp/centraldogma/internal/jsonpatch/CopyOperationSerializationTest.java

-43
This file was deleted.

common/src/test/java/com/linecorp/centraldogma/internal/jsonpatch/CopyOperationTest.java

-43
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2017 LINE Corporation
2+
* Copyright 2020 LINE Corporation
33
*
44
* LINE Corporation licenses this file to you under the Apache License,
55
* version 2.0 (the "License"); you may not use this file except in compliance
@@ -38,79 +38,79 @@
3838

3939
import java.io.IOException;
4040
import java.net.URL;
41-
import java.util.ArrayList;
42-
import java.util.Iterator;
4341
import java.util.List;
42+
import java.util.function.Predicate;
4443

45-
import org.testng.annotations.DataProvider;
46-
import org.testng.annotations.Test;
44+
import org.junit.jupiter.api.Order;
45+
import org.junit.jupiter.params.ParameterizedTest;
46+
import org.junit.jupiter.params.provider.Arguments;
47+
import org.junit.jupiter.params.provider.MethodSource;
4748

4849
import com.fasterxml.jackson.databind.JsonNode;
4950
import com.fasterxml.jackson.databind.ObjectMapper;
5051
import com.google.common.base.Equivalence;
51-
import com.google.common.base.Predicate;
52+
import com.google.common.collect.ImmutableList;
5253

53-
public final class JsonPatchGenerationTest {
54+
class JsonPatchGenerationTest {
5455

56+
private static final ObjectMapper MAPPER = new ObjectMapper();
5557
private static final Equivalence<JsonNode> EQUIVALENCE = JsonNumEquals.getInstance();
5658

57-
private final JsonNode testData;
59+
@Order(1)
60+
@ParameterizedTest
61+
@MethodSource("patches")
62+
void patchAppliesCleanly(JsonNode source, JsonNode target) {
63+
final JsonPatch patch = JsonPatch.generate(source, target, ReplaceMode.SAFE);
64+
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(target)::apply;
65+
final JsonNode actual = patch.apply(source);
5866

59-
public JsonPatchGenerationTest() throws IOException {
60-
final String resource = "/jsonpatch/diff/diff.json";
61-
final URL url = getClass().getResource(resource);
62-
final ObjectMapper objectMapper = new ObjectMapper();
63-
testData = objectMapper.readTree(url);
67+
assertThat(predicate.test(actual))
68+
.withFailMessage("Generated patch failed to apply\nexpected: %s\nactual: %s", target, actual)
69+
.isTrue();
6470
}
6571

66-
@DataProvider
67-
public Iterator<Object[]> getPatchesOnly() {
68-
final List<Object[]> list = new ArrayList<>();
69-
for (final JsonNode node : testData) {
70-
list.add(new Object[] { node.get("first"), node.get("second") });
71-
}
72+
@Order(2)
73+
@ParameterizedTest
74+
@MethodSource("literalPatches")
75+
void expectedPatches(String message, JsonNode source, JsonNode target, JsonNode expected) {
76+
final JsonNode actual = JsonPatch.generate(source, target, ReplaceMode.SAFE).toJson();
77+
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(expected)::apply;
7278

73-
return list.iterator();
79+
assertThat(predicate.test(actual))
80+
.withFailMessage("Patch is not what was expected\nscenario: %s\nexpected: %s\nactual: %s\n",
81+
message, expected, actual)
82+
.isTrue();
7483
}
7584

76-
@Test(dataProvider = "getPatchesOnly")
77-
public void generatedPatchAppliesCleanly(final JsonNode source, final JsonNode target) {
78-
final JsonPatch patch = JsonPatch.generate(source, target, ReplaceMode.SAFE);
79-
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(target);
80-
final JsonNode actual = patch.apply(source);
85+
private static List<Arguments> patches() throws Exception {
86+
final ImmutableList.Builder<Arguments> arguments = ImmutableList.builder();
8187

82-
assertThat(predicate.apply(actual)).overridingErrorMessage(
83-
"Generated patch failed to apply\nexpected: %s\nactual: %s",
84-
target, actual
85-
).isTrue();
88+
for (JsonNode node : getNode()) {
89+
arguments.add(Arguments.of(node.get("first"), node.get("second")));
90+
}
91+
92+
return arguments.build();
8693
}
8794

88-
@DataProvider
89-
public Iterator<Object[]> getLiteralPatches() {
90-
final List<Object[]> list = new ArrayList<>();
91-
for (final JsonNode node : testData) {
95+
private static List<Arguments> literalPatches() throws Exception {
96+
final ImmutableList.Builder<Arguments> arguments = ImmutableList.builder();
97+
98+
for (JsonNode node : getNode()) {
9299
if (!node.has("patch")) {
93100
continue;
94101
}
95-
list.add(new Object[] {
102+
103+
arguments.add(Arguments.of(
96104
node.get("message").textValue(), node.get("first"),
97-
node.get("second"), node.get("patch")
98-
});
105+
node.get("second"), node.get("patch")));
99106
}
100107

101-
return list.iterator();
108+
return arguments.build();
102109
}
103110

104-
@Test(dataProvider = "getLiteralPatches",
105-
dependsOnMethods = "generatedPatchAppliesCleanly")
106-
public void generatedPatchesAreWhatIsExpected(final String message,
107-
final JsonNode source, final JsonNode target,
108-
final JsonNode expected) {
109-
final JsonNode actual = JsonPatch.generate(source, target, ReplaceMode.SAFE).toJson();
110-
final Predicate<JsonNode> predicate = EQUIVALENCE.equivalentTo(expected);
111-
assertThat(predicate.apply(actual)).overridingErrorMessage(
112-
"patch is not what was expected\nscenario: %s\n" +
113-
"expected: %s\nactual: %s\n", message, expected, actual
114-
).isTrue();
111+
private static JsonNode getNode() throws IOException {
112+
final String resource = "/jsonpatch/diff/diff.json";
113+
final URL url = JsonPatchGenerationTest.class.getResource(resource);
114+
return MAPPER.readTree(url);
115115
}
116116
}

0 commit comments

Comments
 (0)