Button in grid is not usable with test().click() (UiUnit Test)

Hi all,

While writing UiUnit tests, I have a problem with TreeGrid, because with test().click() on a Button it returns “is not usable”.

The Problem: The issue is that getCellComponent() returns a Button instance that is not attached to any parent or the active UI state tree. Even though the TreeGrid itself is attached, the ComponentRenderer seems to generate a new, isolated instance for each call.

Debug Observations:

  • Instance Mismatch: Multiple calls to getCellComponent(row, col) return different button objects (firstCall == secondCall is false).
  • Attachment Status: button.isAttached() returns false and button.getParent().isPresent() is also false.
  • Usability Check: Because the button is detached, Vaadin’s internal isInert() logic or usability check fails, throwing: java.lang.IllegalStateException: Button[...] is not usable.

Is this a known behavior in Vaadin’s ComponentRenderer when used with uiunit testing , and is a fix planned to ensure components are automatically attached when retrieved via getCellComponent()?"

grid.addHierarchyColumn(item -> item)
            .setHeader("Header")
            .setSortable(true);

    grid.addColumn(new ComponentRenderer<>(item -> {
              if (item.contains("- Child")) {
                return createButton(item);
              }
              return null;
            })).setHeader("Action")
            .setWidth("10em")
            .setFlexGrow(0);

    grid.setItems(
            List.of("Value 1", "Value 2", "Value 3"),
            parent -> {
              if (!parent.contains("- Child")) {
                return List.of(parent + " - Child 1", parent + " - Child 2", parent + " - Child 3");
              }
              return Collections.emptyList();
            }
    );

    VerticalLayout verticalLayout = new VerticalLayout();
    verticalLayout.setId("vl");
    verticalLayout.add(grid);
    add(verticalLayout);
  }

  Button createButton(String item) {
    Button btn = new Button();
    btn.setText("Button");
    btn.addClickListener(
            click -> {
              showInfoNotification("Clicked");
            });
    btn.setMinWidth("2em");
    btn.setId("btn-" + item);
    return btn;
  }

True, as the component needs to be generated by the lambda provided in addComponentColumn(..) method or ComponentRenderer constructor, which naturally returns a new instance each time called. If your test requires that the instance is same, store reference returned by getCellComponent(..) in a variable.

Sounds like this bug: test(button).click() does not work with buttons rendered in Grid cells. · Issue #1966 · vaadin/testbench · GitHub

So please check that you are using the latest version.

Thanks Tatu, we are currently using version 24.8.3, but we are in the process of changing to 25