### Question:
JSON { public static <T> T getAs(Map<String,Object> json, String key, Class<T> type) throws DataException { final Object o = getNonNull(json, key); if (!type.isInstance(o)) { throw new DataException( key + " == " + o + ", a " + o.getClass().getName() + ", expected a " + type.getName()); } return type.cast(o); } protected  JSON(); static T getAs(Map<String,Object> json, String key, Class<T> type); static Object getNonNull(Map<String,Object> json, String key);  }### Answer:
@Test(expected=JSON.DataException.class) public void testGetAsNullKey() throws JSON.DataException { final Map<String,Object> json = new HashMap<String,Object>(); JSON.getAs(json, null, String.class); }
@Test(expected=JSON.DataException.class) public void testGetAsNullValue() throws JSON.DataException { final Map<String,Object> json = new HashMap<String,Object>(); final String key = "foo"; json.put(key, null); JSON.getAs(json, key, String.class); }
@Test(expected=JSON.DataException.class) public void testGetAsMissingKey() throws JSON.DataException { final Map<String,Object> json = new HashMap<String,Object>(); final String key = "foo"; JSON.getAs(json, key, String.class); }
@Test(expected=JSON.DataException.class) public void testGetAsTypeMismatch() throws JSON.DataException { final Map<String,Object> json = new HashMap<String,Object>(); final String key = "foo"; json.put(key, 42); JSON.getAs(json, key, Map.class); }
@Test public void testGetAsOk() throws JSON.DataException { final Map<String,Object> json = new HashMap<String,Object>(); final String key = "foo"; final Integer val = 42; json.put(key, val); assertEquals(val, JSON.getAs(json, key, Integer.class)); }