### Question:
Binder { public void setupBindings(BindingContext bindingContext) { addFieldBindings(bindingContext); addMethodBindings(bindingContext); } Binder(Object view, Object pmo); void setupBindings(BindingContext bindingContext);  }### Answer:
@Test public void testSetupBindings() { TestView view = new TestView(); TestPmo pmo = new TestPmo(); Binder binder = new Binder(view, pmo); binder.setupBindings(bindingContext); assertThat(pmo.getClickCount(), is(0)); assertThat(pmo.getModel().getClickCount(), is(0)); assertThat(bindingContext.getBindings(), hasSize(2)); assertThat(view.fieldForFieldBinding.getLabelText(), is("")); assertThat(view.fieldForFieldBinding.isEnabled(), is(true)); assertThat(view.fieldForMethodBinding.getLabelText(), is("mod")); assertThat(view.fieldForMethodBinding.isEnabled(), is(false)); assertThat(view.fieldForFieldBinding.getTooltipText(), is(TestPmo.PMO_ATTRIBUTE_TOOLTIP)); assertThat(view.fieldForMethodBinding.getTooltipText(), is(TestPmo.MODEL_ATTRIBUTE_TOOLTIP)); pmo.setModelAttributeEnabled(true); pmo.setModelAttributeTooltip(TestPmo.PMO_ATTRIBUTE_TOOLTIP); bindingContext.modelChanged(); assertThat(view.fieldForMethodBinding.isEnabled(), is(true)); assertThat(view.fieldForMethodBinding.getTooltipText(), is(TestPmo.PMO_ATTRIBUTE_TOOLTIP)); view.fieldForMethodBinding.setEnabled(false); view.fieldForFieldBinding.click(); view.fieldForMethodBinding.click(); view.fieldForMethodBinding.click(); assertThat(pmo.isModelAttributeEnabled(), is(true)); assertThat(pmo.getClickCount(), is(1)); assertThat(pmo.getModel().getClickCount(), is(2)); }
@Test public void testSetupBindings_ThrowsExceptionForNullField() { TestView view = new TestView(); view.fieldForFieldBinding = null; assertThat(view.fieldForFieldBinding, is(nullValue())); Binder binder = new Binder(view, new TestPmo()); Assertions.assertThrows(NullPointerException.class, () -> { binder.setupBindings(bindingContext); }); }
@Test public void testSetupBindings_ThrowsExceptionForMethodReturningNull() { TestView view = new TestView(); view.fieldForMethodBinding = null; assertThat(view.methodForMethodBinding(), is(nullValue())); Binder binder = new Binder(view, new TestPmo()); Assertions.assertThrows(NullPointerException.class, () -> { binder.setupBindings(bindingContext); }); }
@Test public void testSetupBindings_ThrowsExceptionForAnnotatedNonComponentField() { IllegalFieldAnnotationView view = new IllegalFieldAnnotationView(); Binder binder = new Binder(view, new TestPmo()); Assertions.assertThrows(IllegalStateException.class, () -> { binder.setupBindings(bindingContext); }); }
@Test public void testSetupBindings_ThrowsExceptionForAnnotatedMethodWithParameters() { IllegalMethodParamtersView view = new IllegalMethodParamtersView(); Binder binder = new Binder(view, new TestPmo()); Assertions.assertThrows(IllegalArgumentException.class, () -> { binder.setupBindings(bindingContext); }); }
@Test public void testSetupBindings_ThrowsExceptionForAnnotatedMethodWithNonComponentReturnType() { IllegalMethodReturnTypeView view = new IllegalMethodReturnTypeView(); Binder binder = new Binder(view, new TestPmo()); Assertions.assertThrows(IllegalStateException.class, () -> { binder.setupBindings(bindingContext); }); }