Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to unit test with Java Swing JOptionPane Confirm Dialog

I have a java swing application which expects the users to choose YES or NO from JOptionPane.showConfirmDialog

Since the JOptionPane stops the thread and is waiting for user input, my code is not automatically testable.

Is there anyway I can programatically get around this? Or simulate yes or no?

In my test now, a confirm dialog appears, where I have to push the yes or no button.

Update

I found a brilliant option.

I created an interface called OptionPane with basically all the different types of messages I need. Then I created a default implementation that just relegates to the JOptionPane`s static methods. Then I created a YesMockOptionPane, that basically returns YES_OPTION for all the confirm messages, and a NoMockOptionPane for all the NO_OPTIONS.

Here is the code:

<<usage>>

class Foo {
  OptionPane optionPane = new DefaultOptionPane();

  public void someMethod() {
    if (optionPane.showConfirmDialog(null, "choose yes or no", "Please confirm", 
                    JOptionPane.YES_NO_OPTION) != JOptionPane.YES_OPTION)
                return;

        //User pressed yes
  }    

  public void setOptionPane(OptionPane o) {
    this.optionPane = o;
  }
}

//Snippet of the interface
public interface OptionPane {

   int showConfirmDialog(Component parentComponent,
          Object message, String title, int optionType);
}

public class DefaultOptionPane implements OptionPane {
  @Override
  public int showConfirmDialog(Component parentComponent,
        Object message, String title, int optionType) {

      return JOptionPane.showConfirmDialog(parentComponent,message,title,optionType);
  }
}

public class YesMockOptionPane extends DefaultOptionPane {
  //MockOptionPane is just an abstract class implementing default methods from OptionPane
  @Override
  public int showConfirmDialog(Component parentComponent, Object message, String title, int optionType) {
      return JOptionPane.YES_OPTION;
   }
}

Now in the unit test I can simply set the appropriate MockOptionPane.

dialog.setOptionPane(new YesMockOptionPane());
like image 484
Shervin Asgari Avatar asked Jul 17 '12 12:07

Shervin Asgari


People also ask

How do I create a confirm dialog box in Java?

To create a confirmation dialog box in Java, use the Java Swing JOptionPane. showConfirmDialog() method, which allows you to create a dialog box that asks for confirmation from the user.

What are the 4 JOptionPane dialog boxes?

The JOptionPane displays the dialog boxes with one of the four standard icons (question, information, warning, and error) or the custom icons specified by the user.

What is JOptionPane input dialog?

The JOptionPane class is used to provide standard dialog boxes such as message dialog box, confirm dialog box and input dialog box. These dialog boxes are used to display information or get input from the user. The JOptionPane class inherits JComponent class.

Is JOptionPane a swing?

The JOptionPane is a class that is used to provide standard dialog boxes. It is a part of Java Swing which is used for creating window-based applications. JOptionPane is a component from Java Swing and it deals with dialog boxes especially.


1 Answers

The elegant way would be to modify the code to expect the answer from an interface, something like

public interface UserAnswer {
    boolean yesPressed();
}

and then implement it with JOptionPane.showConfirmDialog for production code, and hardcoded for test code.

If this is not possible, you can try to send events on the event dispatch thread to the JOptionPane with EventQueue.invokeLater.

like image 179
lbalazscs Avatar answered Oct 23 '22 09:10

lbalazscs