alexgbelov
alexgbelov

Reputation: 3169

JestResult can't parse my custom JsonObject

I am trying to write unit tests for my JestClient wrapper. To do so, I created a JestResult object where I set a custom JsonObject that has my expected output. However, the JestResult cannot parse my JsonObject. The issue is in the following JestResult method:

protected <T> T createSourceObject(JsonElement source, Class<T> type) {
    T obj = null;
    try {

        String json = source.toString();
        obj = gson.fromJson(json, type);

        // Check if JestId is visible
        Field[] fields = type.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(JestId.class)) {
                try {
                    field.setAccessible(true);
                    Object value = field.get(obj);
                    if (value == null) {
                        Class<?> fieldType = field.getType();
                        JsonElement id = ((JsonObject) source).get(ES_METADATA_ID);
                        field.set(obj, getAs(id, fieldType));
                    }
                } catch (IllegalAccessException e) {
                    log.error("Unhandled exception occurred while getting annotated id from source");
                }
                break;
            }
        }

    } catch (Exception e) {
        log.error("Unhandled exception occurred while converting source to the object ." + type.getCanonicalName(), e);
    }
    return obj;
}

Specifically, the line

obj = gson.fromJson(json, type);

This line sets all fields of obj to null. However, if I use the same line in my own code, all fields are properly initialized. I am using

Jest: 0.1.6

Gson: 2.3.1

Upvotes: 1

Views: 427

Answers (0)

Related Questions