Binder { public void bind(Component componentRoot, Object controller) { if (controller == null) { return; } bindFields(componentRoot, controller, controller.getClass()); bindMethods(componentRoot, controller); } Binder();  Binder(String idPrefix); void bind(Component componentRoot, Object controller); Map<String, Component> getAlreadyAssignedFields(Object controller);  }
@Test public void bind_field_fieldSetCorrectly() { Button button = (Button) inflater.inflate(getXml("single-button.xml")); ControllerWithFieldBinding controller = new ControllerWithFieldBinding(); Binder binder = new Binder(); binder.bind(button, controller); assertSame(button, controller.getMyButton()); }
@Test public void bind_field_fieldOfSuperclassSetCorrectly() { Button button = (Button) inflater.inflate(getXml("single-button.xml")); SubcontrollerWithoutFieldBinding controller = new SubcontrollerWithoutFieldBinding(); Binder binder = new Binder(); binder.bind(button, controller); assertSame(button, controller.getMyButton()); }
@Test(expected = BinderException.class) public void bind_fieldWithMissingId_exceptionThrown() { Button button = (Button) inflater.inflate(getXml("single-button.xml")); ControllerWithMissingIdBinding controller = new ControllerWithMissingIdBinding(); Binder binder = new Binder(); binder.bind(button, controller); }
@Test public void bind_dataSource_dataSourceAttached() { DateField view = (DateField) inflater .inflate(getXml("single-datefield.xml")); Binder binder = new Binder(); binder.bind(view, new ControllerWithDataSource()); Date value = (Date) view.getValue(); assertEquals(1337337477578L, value.getTime()); }
@Test(expected = BinderException.class) public void bind_nonExistingId_exceptionThrown() { Button button = (Button) inflater.inflate(getXml("single-button.xml")); Binder binder = new Binder(); binder.bind(button, new ControllerWithMissingIdBinding()); }
@Test public void bind_withoutId_fieldSetCorrectly() { Button button = (Button) inflater.inflate(getXml("single-button.xml")); ControllerWithFieldBindingWithoutId controller = new ControllerWithFieldBindingWithoutId(); Binder binder = new Binder(); binder.bind(button, controller); assertSame(button, controller.myButton); }
@Test public void bound_event_exceptionHandling_noUndeclaredThrowableOrInvocationTargetException() { Button button = (Button) inflater.inflate(getXml("single-button.xml")); ControllerWithExceptionInClickHandler controller = new ControllerWithExceptionInClickHandler(); Binder binder = new Binder(); binder.bind(button, controller); try { button.click(); } catch (Exception ex) { Throwable current = ex; while (current != null) { assertThat( current, allOf(not(instanceOf(UndeclaredThrowableException.class)), not(instanceOf(InvocationTargetException.class)))); current = current.getCause(); } } }