title: How to test method of JavaFX controller?
body: <p>I was trying to use TestFX to test my application. I would like to run the test for the method of my controller.</p>
  
  <p>Main.java:</p>
  
  
  
  <pre class="lang-java prettyprint-override"><code>public class Main extends Application {
      try{
          new Flow(ManageCtrl.class).startInStage(primaryStage);
      } catch (Exception ex) {
          LOGGER.log(Level.SEVERE, null, ex);
      }
  }
  </code></pre>
  
  <p>ManageCtrl.java:</p>
  
  <pre class="lang-java prettyprint-override"><code>@ViewController("/FPManage.fxml")
  public class ManageCtrl extends AnchorPane {
  
      @FXML // fx:id="email"
      private TextField email; // Value injected by FXMLLoader
  
      public void setEmail(String address) {
          this.email.setText(address);
      }
  }
  </code></pre>
  
  <p>ManageCtrlTest.java:</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ManageCtrlTest extends ApplicationTest {
  
      @Override
      public void start(Stage stage) {
          try {
              new Flow(ManageCtrl.class).startInStage(stage);
          } catch (FlowException ex) {
              Logger.getLogger(ManageCtrlTest.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
      @Test
      public void testSetEmail() {
          ManageCtrl instance = new ManageCtrl();
          instance.setEmail("test@gmai.com");
  
          assertEquals("test@gmail.com", ((TextField)GuiTest.find("#email")).getText());
      }
  }
  </code></pre>
  
  <p>But I get the following Exception:</p>
  
  <pre class="lang-java prettyprint-override"><code>testSetEmail Failed: java.lang.illegalStateException: Not on FX application thread; currentThread = Test worker
  java.lang.illegalStateException: Not on FX application thread; currentThread = Test Worker
  </code></pre>
  
  <p>Thanks for the help.</p>