### Question:
ResponseParser { public static Object toObject(final String response, final Class givenClass) { String[] parameterPairs = response.split("&"); Object obj; try { obj = givenClass.newInstance(); } catch (InstantiationException | IllegalAccessException e) { e.printStackTrace(); throw new RuntimeException("Cannot create object from this" + " response"); } for (String pair: parameterPairs) { try { String fieldName = URLDecoder.decode(CaseFormat.UPPER_UNDERSCORE .to(CaseFormat.LOWER_CAMEL, pair.split("=")[0]), "UTF-8"); String parameterValue = URLDecoder.decode(pair.split("=")[1], "UTF-8"); if (givenClass.getDeclaredField(fieldName).isAccessible()) publicField(fieldName, parameterValue, givenClass, obj); else nonPublicField(fieldName, parameterValue, givenClass, obj); } catch (IllegalAccessException | InvocationTargetException | NoSuchMethodException | UnsupportedEncodingException | NoSuchFieldException e) { e.printStackTrace(); throw new RuntimeException("Cannot create object from this" + " response"); } } return obj; } private  ResponseParser(); static String toURLEncoded(final Class givenClass,
            final Object obj); static String publicField(final Object obj, final Field field); static String nonPublicField(final Class givenClass,
            final Object obj, final Field field); static void publicField(final String parameterName,
            final String parameterValue, final Class givenClass,
            final Object obj); static void nonPublicField(final String parameterName,
            final String parameterValue, final Class givenClass,
            final Object obj); static Object toObject(final String response,
            final Class givenClass); static String encodeParameter(final String parameterName,
            final String parameterValue);  }### Answer:
@Test public void testToObject() { RequestToken token = (RequestToken) ResponseParser.toObject( "request_token=fd%20%20fd&token_secret=fh&authorized=false", RequestToken.class); assertNotNull(token); assertEquals(token.getRequestToken(), "fd fd"); assertEquals(token.getTokenSecret(), "fh"); assertFalse(token.isAuthorized()); ClientCredentials clientCredentials = (ClientCredentials) ResponseParser .toObject("client_id=client%20id&client_secret=client%20secret", ClientCredentials.class); assertNotNull(clientCredentials); assertEquals(clientCredentials.getClientId(), "client id"); assertEquals(clientCredentials.getClientSecret(), "client secret"); EncryptionKey encryptionKey = (EncryptionKey) ResponseParser.toObject( "encryption_key=key%20content", EncryptionKey.class); assertNotNull(encryptionKey); assertEquals(encryptionKey.getEncryptionKey(), "key content"); }
@Test(expected = RuntimeException.class) public void testToObjectRuntimeException() { ResponseParser.toObject("encryption_key=dfds", RequestToken.class); }