Skip to content

Commit e9c9050

Browse files
committed
feat: Add JUnit 5 and AssertJ for testing
This commit introduces a comprehensive testing framework to the project by adding JUnit 5 and AssertJ. Key changes include: - Addition of `junit-jupiter-api`, `junit-jupiter-engine`, `junit-platform-launcher`, and `junit-platform-console` dependencies to `pom.xml`. - Integration of `assertj-core` for fluent assertions. - Configuration of the `maven-surefire-plugin` to execute tests during the build lifecycle. - A new test, `JsonParserTests.java`, has been added to validate the JSON parsing functionality and serve as an example of the testing setup.
1 parent 29ed3fa commit e9c9050

File tree

2 files changed

+93
-0
lines changed

2 files changed

+93
-0
lines changed

pom.xml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,43 @@
2121
<properties>
2222
<maven.compiler.source>21</maven.compiler.source>
2323
<maven.compiler.target>21</maven.compiler.target>
24+
<junit.jupiter.version>5.13.1</junit.jupiter.version>
25+
<junit.platform.version>1.13.1</junit.platform.version>
2426
</properties>
2527

28+
<dependencies>
29+
<dependency>
30+
<groupId>org.junit.jupiter</groupId>
31+
<artifactId>junit-jupiter-api</artifactId>
32+
<version>${junit.jupiter.version}</version>
33+
<scope>test</scope>
34+
</dependency>
35+
<dependency>
36+
<groupId>org.junit.jupiter</groupId>
37+
<artifactId>junit-jupiter-engine</artifactId>
38+
<version>${junit.jupiter.version}</version>
39+
<scope>test</scope>
40+
</dependency>
41+
<dependency>
42+
<groupId>org.junit.platform</groupId>
43+
<artifactId>junit-platform-launcher</artifactId>
44+
<version>${junit.platform.version}</version>
45+
<scope>test</scope>
46+
</dependency>
47+
<dependency>
48+
<groupId>org.junit.platform</groupId>
49+
<artifactId>junit-platform-console</artifactId>
50+
<version>${junit.platform.version}</version>
51+
<scope>test</scope>
52+
</dependency>
53+
<dependency>
54+
<groupId>org.assertj</groupId>
55+
<artifactId>assertj-core</artifactId>
56+
<version>3.26.3</version>
57+
<scope>test</scope>
58+
</dependency>
59+
</dependencies>
60+
2661
<build>
2762
<plugins>
2863
<plugin>
@@ -34,6 +69,11 @@
3469
<target>21</target>
3570
</configuration>
3671
</plugin>
72+
<plugin>
73+
<groupId>org.apache.maven.plugins</groupId>
74+
<artifactId>maven-surefire-plugin</artifactId>
75+
<version>3.2.5</version>
76+
</plugin>
3777
</plugins>
3878
</build>
3979
</project>
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package jdk.sandbox.internal.util.json;
2+
3+
import jdk.sandbox.java.util.json.JsonArray;
4+
import jdk.sandbox.java.util.json.JsonBoolean;
5+
import jdk.sandbox.java.util.json.JsonNumber;
6+
import jdk.sandbox.java.util.json.JsonObject;
7+
import jdk.sandbox.java.util.json.JsonString;
8+
import org.junit.jupiter.api.Test;
9+
import static org.assertj.core.api.Assertions.assertThat;
10+
11+
public class JsonParserTests {
12+
13+
@Test
14+
void testParseComplexJson() {
15+
String json = """
16+
{
17+
"name": "John Doe",
18+
"age": 30,
19+
"isStudent": false,
20+
"courses": [
21+
{"title": "History", "credits": 3},
22+
{"title": "Math", "credits": 4}
23+
],
24+
"address": {
25+
"street": "123 Main St",
26+
"city": "Anytown"
27+
}
28+
}
29+
""";
30+
31+
JsonParser parser = new JsonParser(json.toCharArray());
32+
JsonObject jsonObject = (JsonObject) parser.parseRoot();
33+
34+
assertThat(((JsonString) jsonObject.members().get("name")).value()).isEqualTo("John Doe");
35+
assertThat(((JsonNumber) jsonObject.members().get("age")).toNumber().longValue()).isEqualTo(30L);
36+
assertThat(((JsonBoolean) jsonObject.members().get("isStudent")).value()).isFalse();
37+
38+
JsonArray courses = (JsonArray) jsonObject.members().get("courses");
39+
assertThat(courses.values()).hasSize(2);
40+
41+
JsonObject course1 = (JsonObject) courses.values().get(0);
42+
assertThat(((JsonString) course1.members().get("title")).value()).isEqualTo("History");
43+
assertThat(((JsonNumber) course1.members().get("credits")).toNumber().longValue()).isEqualTo(3L);
44+
45+
JsonObject course2 = (JsonObject) courses.values().get(1);
46+
assertThat(((JsonString) course2.members().get("title")).value()).isEqualTo("Math");
47+
assertThat(((JsonNumber) course2.members().get("credits")).toNumber().longValue()).isEqualTo(4L);
48+
49+
JsonObject address = (JsonObject) jsonObject.members().get("address");
50+
assertThat(((JsonString) address.members().get("street")).value()).isEqualTo("123 Main St");
51+
assertThat(((JsonString) address.members().get("city")).value()).isEqualTo("Anytown");
52+
}
53+
}

0 commit comments

Comments
 (0)