title: Simulate a mouse click during test. JUnit, JavaFX
body: <p>In my test method, I'm trying to simulate a mouse click using Robot class:</p>
  
  <pre><code>@Test
  public void testMouseEvents(){
      clickMouse();
  }
  
  private void clickMouse() {
      final boolean[] flag = {true};
      Platform.runLater(() -&gt; {
          try {
              Robot robot = new Robot();
              robot.mouseMove(900, 500);
              robot.delay(2000);
              robot.mousePress(InputEvent.BUTTON1_DOWN_MASK);
              robot.delay(2000);
              robot.mouseRelease(InputEvent.BUTTON1_DOWN_MASK);
              robot.delay(2000);
          } catch (Exception ignored) {
  
          }
          flag[0] = false;
      });
  
      try {
          while (flag[0]) {
              Thread.sleep(3);
          }
          Thread.sleep(20);
      } catch (InterruptedException e) {
          e.printStackTrace();
      }
  }
  </code></pre>
  
  <p>But the last method throws different exeptions: sometimes it's NullPointerm but more often it's:</p>
  
  <pre><code>java.util.concurrent.RejectedExecutionException: Task     com.sun.javafx.tk.quantum.PaintRenderJob@24f7eafc rejected from  com.sun.javafx.tk.quantum.QuantumRenderer@14a5ef25[Terminated, pool size = 0, active threads = 0, queued tasks = 0, completed tasks = 4]
  at java.util.concurrent.ThreadPoolExecutor$AbortPolicy.rejectedExecution(ThreadPoolExecutor.java:2047)
  at java.util.concurrent.ThreadPoolExecutor.reject(ThreadPoolExecutor.java:823)
  at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1369)
  at java.util.concurrent.AbstractExecutorService.submit(AbstractExecutorService.java:112)
  </code></pre>
  
  <p>Curiously, but when I simulate KeyEvent, it works perfectly. Moreover, if I delete the line where the mouse key is supposed to be pressed, the robot moves the cursor and no exception is thrown.
  I suspect that the issue might concern the thread in which JavaFX application works. But still I can't figure out why it doesn't work. </p>