When developing a Java desktop application with Swing, I was faced with the need to directly test the interface, and not just the base classes of controllers / models using unit tests.
This answer (on the topic βWhat is the best testing tool for Swing applications?β) Suggested using FEST , which, unfortunately, has been discontinued. However, there are several projects that continued from where FEST remained. One of them (mentioned in this answer ) caught my attention since I used it before in unit tests: AssertJ .
There seems to be AssertJ Swing , which is based on FEST and offers some easy ways to write Swing UI tests. Nevertheless, the transition to the initial / operational setting is cumbersome, since it is difficult to say where to start.
How to create a minimal test setup for the following UI example consisting of only two classes?
Limitations: Java SE, Swing UI, Maven Project, JUnit
public class MainApp { public static void main(String[] args) { MainApp.showWindow().setSize(600, 600); } public static MainWindow showWindow() { MainWindow mainWindow = new MainWindow(); mainWindow.setVisible(true); return mainWindow; } }
public class MainWindow extends JFrame { public MainWindow() { super("MainWindow"); this.setContentPane(this.createContentPane()); } private JPanel createContentPane() { JTextArea centerArea = new JTextArea(); centerArea.setName("Center-Area"); centerArea.setEditable(false); JButton northButton = this.createButton("North", centerArea); JButton southButton = this.createButton("South", centerArea); JPanel contentPane = new JPanel(new BorderLayout()); contentPane.add(centerArea); contentPane.add(northButton, BorderLayout.NORTH); contentPane.add(southButton, BorderLayout.SOUTH); return contentPane; } private JButton createButton(final String text, final JTextArea centerArea) { JButton button = new JButton(text); button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent event) { centerArea.setText(centerArea.getText() + text + ", "); } }); return button; } }
I know that the question itself is very broad, so I myself give the answer - to show this specific example.
java swing ui-testing assertj
Carsten Oct 10 '17 at 22:01 2017-10-10 22:01
source share