title: How can I write a JUnit test on a button that makes a modal JDialog?
body: <p>I am currently working on a java swing application that get several inputs from the user. At one point I have a <code>JButton</code> who's action listener spawns a modal <code>JDialog</code> frame where the user will input data. The button then takes the data and does a bunch of stuff with it. The issue is that I need to test the <code>JButton</code> to make sure it functions properly but a unit test will stop as soon as it spawns the new Frame since it is modal. How can I test that the button does in fact spawn a new window, and that the rest of the buttons functions work?</p>
  
  <p><strong>Note:</strong></p>
  
  <ul>
  <li>I cant <code>frame.setModal(false);</code> because it is contained in the actionlistner.</li>
  <li>I cant add a check to change if it is modal depending on where the call came from because it would defeat the point of writing a test </li>
  <li>I cant simply make the frame permanently not modal because I want to make sure the user does not interact with the other frames while the dialog is open.</li>
  </ul>
  
  <p><strong>Example code:</strong> </p>
  
  <pre><code>    public void actionPerformed(ActionEvent e) {
          InputDialog inputDialog = new InputDialog(button);
          String value = inputDialog.open();
  
          // I then do a bunch of stuff with value
      }
  </code></pre>
  
  <p>-</p>
  
  <pre><code>    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);
          }
  
  
      }
  </code></pre>