This commit is related to BAEL-7331 (#15624)

This commit aims to add a new test class "DeprecatedJsonParserUnitTest".
This commit is contained in:
MohamedHelmyKassab 2024-01-14 00:58:40 +02:00 committed by GitHub
parent f75ab1c847
commit 797f1e1737
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 42 additions and 0 deletions

View File

@ -0,0 +1,42 @@
package com.baeldung.deprecatedjsonparser;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.stream.JsonReader;
import org.junit.jupiter.api.Test;
import java.io.StringReader;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class DeprecatedJsonParserUnitTest {
String jsonString = "{\"name\": \"John\", \"age\":30, \"city\":\"New York\"}";
JsonObject expectedJsonObject = new JsonObject();
DeprecatedJsonParserUnitTest() {
expectedJsonObject.addProperty("name", "John");
expectedJsonObject.addProperty("age", 30);
expectedJsonObject.addProperty("city", "New York");
}
@Test
public void givenJsonString_whenUsingParseString_thenJsonObjectIsExpected() {
JsonObject jsonObjectAlt = JsonParser.parseString(jsonString).getAsJsonObject();
assertEquals(expectedJsonObject, jsonObjectAlt);
}
@Test
public void givenJsonString_whenUsingParseReader_thenJsonObjectIsExpected() {
StringReader reader = new StringReader(jsonString);
JsonObject jsonObject = JsonParser.parseReader(reader).getAsJsonObject();
assertEquals(expectedJsonObject, jsonObject);
}
@Test
public void givenJsonReader_whenParseUsingJsonReader_thenJsonObjectIsExpected() {
JsonReader jsonReader = new JsonReader(new StringReader(jsonString));
JsonObject jsonObject = JsonParser.parseReader(jsonReader).getAsJsonObject();
assertEquals(expectedJsonObject, jsonObject);
}
}