Created
April 13, 2024 11:52
-
-
Save phpmaps/c37fa005db0f277e7fad689d36549d16 to your computer and use it in GitHub Desktop.
Java Test with JSON
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import org.junit.jupiter.api.Test; | |
import org.skyscreamer.jackson.databind.ObjectMapper; | |
import static org.junit.jupiter.api.Assertions.*; | |
public class JsonObjectPropertyTest { | |
@Test | |
public void testJsonObjectHasProperty() throws Exception { | |
String jsonString = "{\"name\": \"John Doe\", \"age\": 30}"; | |
ObjectMapper mapper = new ObjectMapper(); | |
// Parse JSON string to a JsonObject | |
Object jsonObject = mapper.readValue(jsonString, Object.class); | |
// Assert that the property "name" exists | |
assertTrue(jsonObject instanceof Map, "Object should be a Map"); | |
@SuppressWarnings("unchecked") | |
Map<String, Object> map = (Map<String, Object>) jsonObject; | |
assertTrue(map.containsKey("name"), "JsonObject should have a property named 'name'"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment