When a matching component could not be found, regardless of the lookup type, the thrown
ComponentLookupException includes a formatted component hierarchy that can help us determine why the component lookup failed.
For our example, we are going to use a frame of type
org.fest.swing.test.TestFrame that contains a
JButton with name 'click'. The following code listing shows a mistake we made when looking up that button in our test. We accidentally used the name 'ok' instead of 'click':
FrameFixture frame = new FrameFixture(new TestFrame()); frame.show(); frame.button("ok").click();
When we run our test, it will obviously fail, because the frame does not have any button with name 'ok.' To help us diagnose the problem, the thrown
ComponentLookupException displays a nicely formatted component hierarchy:
org.fest.swing.exception.ComponentLookupException: Unable to find component using matcher org.fest.swing.core.NameMatcher[name='ok', requireShowing=false]. Component hierarchy: org.fest.swing.test.TestFrame[name='testFrame', title='Test', enabled=true, showing=true] javax.swing.JRootPane javax.swing.JPanel[name='null.glassPane'] javax.swing.JLayeredPane javax.swing.JPanel[name='null.contentPane'] javax.swing.JButton[name='click', text='Click Me', enabled=true] at org.fest.swing.core.BasicComponentFinder.componentNotFound(BasicComponentFinder.java:81)
The displayed component hierarchy, unlike the default
toString method in Swing components, does not contain any information related to how the components look (e.g. component sizes, colors, layouts, etc.) That information is pretty much useless, since functional testing focuses on the behavior of the GUI. Instead, FEST-Swing uses custom formatters to show properties that can help us solve problems in our functional tests. For more details about custom component formatters (including how to override the provided ones with your own,) please visit the Component Formatters section.