### Question:
ModelObjects { public static Supplier<Object> supplierFor(Object pmo, String modelObjectName) { Member accessMember = getModelObjectAccessMember(pmo, modelObjectName) .orElseThrow(() -> ModelObjectAnnotationException.noAnnotatedMember(pmo.getClass(), modelObjectName)); return () -> MemberAccessors.getValue(pmo, accessMember); } private  ModelObjects(Class<?> pmoClass, String modelObjectName); static Supplier<Object> supplierFor(Object pmo, String modelObjectName); static boolean isAccessible(Object pmo, String modelObjectName);  }### Answer:
@Test public void testGetModelObjectSupplier_noAnnotation() { Assertions.assertThrows(ModelObjectAnnotationException.class, () -> { ModelObjects.supplierFor(new TestObject(), ModelObject.DEFAULT_NAME); }); }
@Test public void testGetModelObjectSupplier_ThrowsExceptionIfNoMatchingAnnotationExists() { Assertions.assertThrows(ModelObjectAnnotationException.class, () -> { ModelObjects.supplierFor(new PmoWithNamedModelObject(), "someOtherName"); }); }
@Test public void testGetModelObjectSupplier_ThrowsExceptionIfAnnotatedMethodReturnsVoid() { Assertions.assertThrows(ModelObjectAnnotationException.class, () -> { ModelObjects.supplierFor(new PmoWithVoidModelObjectMethod(), ModelObject.DEFAULT_NAME); }); }
@Test public void testGetModelObjectSupplier() { Supplier<?> modelObjectSupplier = ModelObjects .supplierFor(new PmoWithNamedModelObject(), PmoWithNamedModelObject.MODEL_OBJECT); assertThat(modelObjectSupplier, is(notNullValue())); assertThat(modelObjectSupplier.get(), is(instanceOf(TestObject.class))); }
@Test public void testModelObjectAnnotatedField() { PmoWithModelObjectField pmoWithModelObjectField = new PmoWithModelObjectField(); assertThat(ModelObjects.supplierFor(pmoWithModelObjectField, ModelObject.DEFAULT_NAME) .get(), is(pmoWithModelObjectField.testSub)); }
@Test public void testPrivateModelObjectAnnotatedFieldInSuperclass() { PmoWithModelObjectFieldInSuperclass pmoWithModelObjectFieldInSuperclass = new PmoWithModelObjectFieldInSuperclass(); Object modelObject = ModelObjects .supplierFor(pmoWithModelObjectFieldInSuperclass, ModelObject.DEFAULT_NAME).get(); assertThat(modelObject, is(not(nullValue()))); assertThat(modelObject, instanceOf(TestSub.class)); }
@Test public void testMixedModelObjectFieldAndMethod() { PmoWithNamedModelObject pmoWithNamedModelObject = new PmoWithNamedModelObject(); Object defaultModelObject = ModelObjects .supplierFor(pmoWithNamedModelObject, ModelObject.DEFAULT_NAME).get(); Object namedModelObject = ModelObjects .supplierFor(pmoWithNamedModelObject, PmoWithNamedModelObject.MODEL_OBJECT).get(); assertThat(defaultModelObject, instanceOf(TestSub.class)); assertThat(namedModelObject, instanceOf(TestObject.class)); }
@Test public void testTwoDefaultModelObjectAnnotations() { PmoWithTwoDefaultModelObjects pmoWithTwoDefaultModelObjects = new PmoWithTwoDefaultModelObjects(); Assertions.assertThrows(ModelObjectAnnotationException.class, () -> { ModelObjects.supplierFor(pmoWithTwoDefaultModelObjects, ModelObject.DEFAULT_NAME) .get(); }); }
@Test public void testTwoDefaultModelObjectMethodAnnotations() { PmoWithTwoDefaultModelObjectMethods pmoWithTwoDefaultModelObjects = new PmoWithTwoDefaultModelObjectMethods(); Assertions.assertThrows(ModelObjectAnnotationException.class, () -> { ModelObjects.supplierFor(pmoWithTwoDefaultModelObjects, ModelObject.DEFAULT_NAME) .get(); }); }