Search code examples
javaswingjunittooltipassertj

JUnit test JTable cell tooltips


I am building a JTable with a custom TableCellRenderer that sets a tooltip to some cells. The tooltip gives further information about the cell's content, so it is a different one for each cell that has it.

For testing, I am using AssertJ and JUnit.

How could I test that the correct tooltips are shown? I came up with two approaches:

  1. Get the components created by the TableCellRenderer and check their tooltips -- unfortunately, even if I use names for each one, they cannot be found.

  2. Automatically place the mouse over the cells and check if there is a tooltip with the desired content. Unfortunately, I do not have any idea of how to do that.

  3. ...?


Solution

  • Something like this

    TableCellRenderer r = myTable.getCellRenderer(row, column);
    JComponent c = (JComponent) myTable.prepareRenderer(r, row, column);
    String tooltip = c.getToolTip();