FieldAccessor { public TYPE get() { TYPE ret = null; Object object = viewWeakReference.get(); if (object != null) { field.setAccessible(true); try { Object returnedObject = field.get(object); if (clazz.isInstance(returnedObject)) { ret = clazz.cast(returnedObject); } } catch (IllegalAccessException e) { Log.w(TAG, "Illegal access", e); } } return ret; } FieldAccessor(Object object, String name, Class<TYPE> clazz); void set(TYPE value); TYPE get();  }
@Test public void givenAnObjectThenFileAccessorReturnsCorrectValue() { FieldAccessor<Integer> fieldAccessor = new FieldAccessor<>(floatingActionButton, "privateField", Integer.class); int value = fieldAccessor.get(); assertThat(value).isEqualTo(FloatingActionButton.DEFAULT); }