### Question:
BodyParserJson implements BodyParser { public <T> T invoke(Context context, Class<T> classOfT) { return invoke(context, classOfT, null); }  T invoke(Context context, Class<T> classOfT); T invoke(Context context, Class<T> classOfT, Type genericType); @Override T invoke(byte[] bytes, Class<T> classOfT); List<String> getContentTypes();  }### Answer:
@Test public void testParsing() { FakeContext context = new FakeContext().setBody("{" + "\"name\":\"wisdom\"," + "\"friends\":[\"clement\",\"jonathan\"]," + "\"age\":2}"); Data data = parser.invoke(context, Data.class); assertThat(data.getName()).isEqualTo("wisdom"); assertThat(data.getFriends()).containsExactly("clement", "jonathan"); assertThat(data.getAge()).isEqualTo(2); }
@Test public void testParsingWithGenericType() throws NoSuchFieldException { Type type = this.getClass().getDeclaredField("tmp").getGenericType(); FakeContext context = new FakeContext().setBody("[{" + "\"name\":\"wisdom\"," + "\"friends\":[\"clement\",\"jonathan\"]," + "\"age\":2}]"); List<Data> list = parser.invoke(context, List.class, type); assertThat(list).hasSize(1); assertThat(list.get(0)).isInstanceOf(Data.class); Data data = list.get(0); assertThat(data.getName()).isEqualTo("wisdom"); assertThat(data.getFriends()).containsExactly("clement", "jonathan"); assertThat(data.getAge()).isEqualTo(2); }
@Test public void testParsingFromByteArray() { String body = "{" + "\"name\":\"wisdom\"," + "\"friends\":[\"clement\",\"jonathan\"]," + "\"age\":2}"; Data data = parser.invoke(body.getBytes(Charsets.UTF_8), Data.class); assertThat(data.getName()).isEqualTo("wisdom"); assertThat(data.getFriends()).containsExactly("clement", "jonathan"); assertThat(data.getAge()).isEqualTo(2); }
@Test public void testNullAndEmpty() throws NoSuchFieldException { Type type = this.getClass().getDeclaredField("tmp").getGenericType(); FakeContext context = new FakeContext().setBody(""); List<Data> data = parser.invoke(context, List.class, type); assertThat(data).isNull(); context = new FakeContext().setBody(null); data = parser.invoke(context, List.class, type); assertThat(data).isNull(); context = new FakeContext().setBody(null); data = parser.invoke(context, List.class); assertThat(data).isNull(); context = new FakeContext().setBody(""); data = parser.invoke(context, List.class); assertThat(data).isNull(); }