title: How to trigger a KeyPressed event for unit testing in JAVA
body: <p>Ok, so I'm trying to Unit test some GUI elements I have a JButton with text of "Create" and the mnemonic set on 'C' or code 67.  I can write a unit test that I can call the .doClick function on and assert that what that button is supposed to do, happens.  </p>
  
  <p>However, I now want to write a unit test to test that the mnemonic portion of that same button works identically to the button click.  I've searched on and off for a couple of days now and have tried many things.  </p>
  
  <p>I've tried using Robot from the awt library:</p>
  
  <pre><code>robot.keyPress(KeyEvent.VK_C);
  robot.keyRelease(KeyEvent.VK_C);
  </code></pre>
  
  <p>To which no event is being triggered and most recently I've tried this EventQueue example:</p>
  
  <pre><code>final Toolkit toolkit = Toolkit.getDefaultToolkit();
  final EventQueue eventQueue = toolkit.getSystemEventQueue();
  gPane = new SwingBuilder();
  panel = new MainMenuPanel();
  panel.displayMainMenu(gPane); 
  
  @Test
  void ShouldMakeRaceGenderPanelVisibleKeyPress(){
  eventQueue.postEvent( new KeyEvent(panel.gPane.mainM, KeyEvent.KEY_PRESSED, System.currentTimeMillis(), 0, KeyEvent.VK_C ).getKeyCode());
  
      assertThat panel.gPane.mainM.isVisible(), is(false);
      assertThat panel.gPane.raceGender.isVisible(), is(true);
  }
  </code></pre>
  
  <p>I know that GUI testing can be tricky and there are tools out there to simulate user interaction, but is it really that hard to simulate or trigger just a single KeyEvent?</p>
  
  <p>Any assistance is welcome, and any UI testing tools recommended is welcome as well.</p>
  
  <p>Thanks</p>
  
  <p>Note: This is a combination of Java and Groovy, so I'm using SwingBuilder to build my UI's </p>