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이 있습니다. 하지만 초기/작업 설정을 시작하는 것은 어디에서 시작해야하는지 말하기가 번거롭기 때문에 어렵습니다.
어떻게는 두 개의 클래스로 구성, 다음 예제 UI에 대한 최소한의 테스트 설정을 만들 수 있습니까?
제약 : 자바 SE, 스윙 UI, 메이븐 프로젝트, 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;
}
}
내가 그러므로 내가 대답을 제공, 질문 자체가 매우 광범위한 것으로 알고 있어요 나 자신 -이 특정 사례를 보여줍니다.
이 질문에 대한 답변으로이 답변에 대한 의견으로 생성되었습니다. https://stackoverflow.com/a/80222/5127499 – Carsten