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());
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With