SwingでJavaデスクトップアプリケーションを開発している間、単体テストを介して基礎となるコントローラ/モデルクラスだけでなく、UIを直接テストする必要がありました。始める方法:AssertJ Swingを使用したJava Swing GUIのテスト
このanswer (on "What is the best testing tool for Swing-based applications?")は、残念ながら廃止されたFESTを使用して提案されています。 しかし、FESTがどこに残っていたのか続くプロジェクトがいくつかあります。私が単体テストでこれまで使用していたように、特に(このanswerに記載されている)私の注意が引かれました:AssertJ。
明らかにFESTに基づいており、Swing UIテストを書くのに使いやすい方法がいくつかあります。AssertJ Swingがあります。 しかし、初期/作業設定に入るのは、どこから始めるのが難しいので、扱いにくいです。私は2つのクラスだけからなる、次の例のUIのための最小限のテスト・セットアップを作成するにはどうすればよい
?
制約:Java SEの、SwingのUI、Mavenのプロジェクト、JUnitの
public class MainApp {
/**
* Run me, to use the app yourself.
*
* @param args ignored
*/
public static void main(String[] args) {
MainApp.showWindow().setSize(600, 600);
}
/**
* Internal standard method to initialize the view, returning the main JFrame (also to be used in automated tests).
*
* @return initialized JFrame instance
*/
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;
}
}
私はので、私は答えを提供し、質問自体が非常に広いであることを承知しています私自身 - この特定の例を示しています。
このQ&Aは、この回答に対するコメントのリクエストとして作成されました。https://stackoverflow.com/a/80222/5127499 – Carsten