2

I'm trying to write some GUI and integration tests using JUnit and FEST. Here is what I have:

@Before
public void setUp(){
    try{
        program.main(args);
        robot.wait(30000);    //gives IllegalMonitorStateException
        Thread.sleep(30000);  //no Exception occurs here
    } catch (Exception e){
        e.printStackTrace();
    }
}

robot and args are already initialized.

Why do I get such an exception when I call wait? Why I don't get the same exception when I call sleep?

eltabo
  • 3,749
  • 1
  • 21
  • 33

3 Answers3

4

You're calling Object.wait() - which is not the same as Thread.sleep(). In particular:

  • wait() requires that you already own the monitor on the object you call it on
  • wait() allows the thread to be notified (via Object.notify/notifyAll) and wake up early; Thread.sleep() would require the thread to be interrupted.
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
0

the wait() method is used in conjunction with the notify() method to synchronize threads. It is not used to delay in a given thread for some amount of time.

You get that exception because in order to wait() on a thread, you must have taken that thread's monitor first (via some kind of synchronized block or method).

Use Thread.sleep().

dlev
  • 48,024
  • 5
  • 125
  • 132
0

Maybe you are looking for

robot.delay(...);
camickr
  • 321,443
  • 19
  • 166
  • 288
  • I'm using [FEST](http://easytesting.org/swing/apidocs/index.html) to access the robot. Does it matter which one I use? –  Jul 14 '11 at 15:23
  • Ah, FEST has a different [Robot](http://easytesting.org/swing/apidocs/org/fest/swing/core/Robot.html) API than java.awt.[Robot](http://download.oracle.com/javase/6/docs/api/java/awt/Robot.html) – Atreys Jul 14 '11 at 15:31