I want to avoid sleeping snail in my swing unit tests, but I don't see a way to do this with events. Currently I have somethink like
WindowWithKeyListener window = new WindowWithKeyListener();
Robot robot = new Robot();
robot.keyPress(KeyEvent.VK_1);
robot.keyRelease(KeyEvent.VK_1);
assertTrue(window.key1pressed());
robot.keyPress(KeyEvent.VK_2);
robot.keyRelease(KeyEvent.VK_2);
assertTrue(window.key2pressed());
This doesn't work without Thread.sleep()
before the assertions, as the events are to slow. But how could I avoid Thread.sleep()
here?
Swing Timers cannot be executed in a specific order. What is the best approach for testing here?