私は現在、ユーザーからいくつかの入力を取得するJavaスイングアプリケーションに取り組んでいます。ある時点で、私はJButton
を持っています。アクションリスナーは、ユーザがデータを入力するモーダルJDialog
フレームを生成します。ボタンはデータを受け取り、それを使ってたくさんのことを行います。問題は、正しく機能することを確認するためにJButton
をテストする必要があることですが、ユニットテストはモーダルなので新しいフレームを生成するとすぐに停止します。ボタンが実際に新しいウィンドウを生成し、残りのボタン機能が機能することをテストするにはどうすればよいですか?モーダルJDialogを作成するボタンにJUnitテストを書き込むにはどうすればよいですか?
注:それはactionlistnerに含まれているため
- 私は
frame.setModal(false);
を傾けます。 - 私はテストを書くことのポイントを敗北させるので、呼び出しがどこから来たのかに応じてモーダルであれば変更することができません
- 私は単純にフレームを永久にモーダルにしません。ダイアログが開いている間、ユーザーは他のフレームと対話しません。
例コード:
public void actionPerformed(ActionEvent e) {
InputDialog inputDialog = new InputDialog(button);
String value = inputDialog.open();
// I then do a bunch of stuff with value
}
-
public class InputDialog extends JDialog {
private JTextField field;
public String getInput() {
this.setVisible(true);
String result = field.getText();
return result;
}
public InputDialog(Component c){
// set the window settings
this.setUndecorated(true); // Remove title bar
this.setLayout(null);
this.setModal(true); // stops interactions with other windows
this.setLocationRelativeTo(c);
this.setSize(200, 125);
// make panel
JPanel panel = new JPanel();
panel.setBounds(0, 0, 200, 125);
panel.setLayout(null);
panel.setBackground(new Color(197, 211, 234));
panel.setBorder(BorderFactory.createEtchedBorder(EtchedBorder.RAISED));
// set up field
field = new JTextField();
field.setBounds(25, 25, 150, 25);
panel.add(field);
// set up enter button
JButton button = new JButton("Submit Value");
button.setBounds(25, 75, 150, 25);
button.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
me.dispose();
}
});
panel.add(button);
this.add(panel);
}
}
ボタンではなく、 'ActionListener'をテストする必要があります。 –
**更新:解決済み**スーパージャンクですが、ボタンの絶対的なXとYを取得し、 'Robot.mousePress()' + 'Robot.mouseRelease()'を使用して、参照。次に、開いたウィンドウを 'Window.getWindows()'で見つけ出し、適切な型にキャストします。これは、スポーンされたオブジェクトへの作業参照を提供します! :) –