Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JUnit and JFileChoosers

I'm having issues with some test cases that make use of JFileChoosers. I'm looking for a way to programmatically get rid of file chooser windows (instead of pressing ESC 7 times) when running the JUnit tests.

I've tried to include the following in my test case:

Robot robot = new Robot();
robot.delay(1000);
robot.keyPress(KeyEvent.VK_ESCAPE);

This does not seem to work. Do you have a suggestion?

Thanks in advance.

like image 956
Kukiwon Avatar asked Dec 06 '25 14:12

Kukiwon


1 Answers

Just a guess, but sounds like you're running the Robot on the same thread as you're launching the JFileChooser. If memory serves, a lot of the JFileChooser methods block the current thread until the user has selected a file.

Try launching the Robot in a separate thread if you aren't already.

EDIT:

For example:

// Start Robot in a new thread.
new Thread(new Runnable() {
    @Override
    public void run() {
        Robot robot = new Robot();
        robot.delay(1000);
        robot.keyPress(KeyEvent.VK_ESCAPE);
    }
}).start();

// Launch JFileChooser.
jFileChooser.getSelectedFile();
like image 180
vaughandroid Avatar answered Dec 08 '25 02:12

vaughandroid



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!