Skip to content

Instantly share code, notes, and snippets.

@phpmaps
Created April 13, 2024 11:52
Show Gist options
  • Save phpmaps/c37fa005db0f277e7fad689d36549d16 to your computer and use it in GitHub Desktop.
Save phpmaps/c37fa005db0f277e7fad689d36549d16 to your computer and use it in GitHub Desktop.
Java Test with JSON
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