### Question:
ReflectionUtil { static <T> Map<String, PropertyAccessor<?>> getPropertyAccessors(Class<T> type) { Set<String> transientProps = new HashSet<>(); Map<String, PropertyAccessor<?>> propertyAccessors = new HashMap<>(); initMethods(type, transientProps, propertyAccessors); initFields(type, transientProps, propertyAccessors); return propertyAccessors; } private  ReflectionUtil();   }### Answer:
@Test public void testCreate() { Map<String, ReflectionUtil.PropertyAccessor<?>> propertyAccessors = ReflectionUtil.getPropertyAccessors(TestBean.class); String[] expectedKeys = { "init", "port", "nastav", "valueField", "value", "set" }; assertAll( () -> assertThat(propertyAccessors.keySet(), hasSize(expectedKeys.length)), () -> assertThat(propertyAccessors.keySet(), containsInAnyOrder(expectedKeys)) ); }
@Test public void testCreateAndCallSetter() throws Throwable { Map<String, ReflectionUtil.PropertyAccessor<?>> propertyAccessors = ReflectionUtil.getPropertyAccessors(TestBean.class); TestBean bean = new TestBean(); assertThat(bean.getValue(), is(nullValue())); assertThat(propertyAccessors.get("value").handle().type().parameterType(1), equalTo(String.class)); assertThat(propertyAccessors.get("init").handle().type().parameterType(1), equalTo(String.class)); propertyAccessors.get("value").handle() .invoke(bean, "val1"); assertThat(bean.getValue(), is("val1")); propertyAccessors.get("init").handle() .invoke(bean, "val2"); assertThat(bean.getInit(), is("val2")); }
@Test public void testCreateAndSetField() throws Throwable { Map<String, ReflectionUtil.PropertyAccessor<?>> propertyAccessors = ReflectionUtil.getPropertyAccessors(TestBean.class); TestBean bean = new TestBean(); assertThat(bean.valueField, is(nullValue())); assertThat(propertyAccessors.get("valueField").handle().type().parameterType(1), equalTo(String.class)); propertyAccessors.get("valueField").handle() .invoke(bean, "val1"); assertThat(bean.valueField, is("val1")); }
@Test public void testCreateAndCallSetterListParam() throws Throwable { Map<String, ReflectionUtil.PropertyAccessor<?>> propertyAccessors = ReflectionUtil.getPropertyAccessors(ListBean.class); ListBean bean = new ListBean(); assertThat(bean.getList(), is(nullValue())); assertThat(propertyAccessors.get("list").handle().type().parameterType(1), equalTo(List.class)); propertyAccessors.get("list").handle() .invoke(bean, List.of(23L, 42L)); assertThat(bean.getList(), contains(23L, 42L)); }
@Test public void testCreateAndSetFieldListParam() throws Throwable { Map<String, ReflectionUtil.PropertyAccessor<?>> propertyAccessors = ReflectionUtil.getPropertyAccessors(ListBean.class); ListBean bean = new ListBean(); assertThat(bean.listField, is(nullValue())); assertThat(propertyAccessors.get("listField").handle().type().parameterType(1), equalTo(List.class)); propertyAccessors.get("listField").handle() .invoke(bean, List.of(23L, 42L)); assertThat(bean.listField, contains(23L, 42L)); }
@Test public void testCreateErrorMethodTransientFieldClash() { ConfigException ex = assertThrows(ConfigException.class, () -> { ReflectionUtil.getPropertyAccessors(MethodTransientFieldClashBean.class); }); assertThat(ex.getMessage(), stringContainsInOrder(List.of("@Value", "method", "@Transient", "prop1"))); }
@Test public void testCreateErrorClashFieldTransientMethodClash() { ConfigException ex = assertThrows(ConfigException.class, () -> { ReflectionUtil.getPropertyAccessors(FieldTransientMethodClashBean.class); }); assertThat(ex.getMessage(), stringContainsInOrder(List.of("@Value", "@Transient", "method", "prop1", "property"))); }