I have been working with different ideas for Test Driven Swing code for a while now. Recently, Suneth and I came up with a new strategy for testing Swing BorderLayouts (unfortunately requiring reflection).
Here is what we did.
When using a border layout you are specifying the relative position of the components. To TDD this, you really want to say whether your component appears in the NORTH, SOUTH, EAST or west location.
Unfortunately this isn't exposed directly, hence the need for reflection.
Here is the method that we wrote to do this:
private JComponent getComponentAt(BorderLayout layout, String position)
throws NoSuchMethodException, IllegalAccessException, InvocationTargetException {
Method method = BorderLayout.class.
getDeclaredMethod("getChild", new Class[]{String.class, boolean.class});
method.setAccessible(true);
return (JComponent) method.invoke(layout, new Object[]{position, Boolean.TRUE});
}
The layout is the layout containing the component, and the string position needs to be one from the BorderLayout constants (BorderLayout.NORTH etc).
The method will retrieve the component at the specified position using reflection, allowing you to write simple assertions around this.