title: How to set 'headless' property in a Spring Boot test?
body: <p>I am testing using Spring Boot with JavaFX (Based on <a href="https://www.youtube.com/watch?v=hjeSOxi3uPg" rel="noreferrer">some excellent YouTube videos</a> that explain this).</p>
  
  <p>To make it work with <a href="https://github.com/TestFX/TestFX" rel="noreferrer">TestFX</a>, I need to create the context like this:</p>
  
  <pre><code>@Override
  public void init() throws Exception {
      SpringApplicationBuilder builder = new SpringApplicationBuilder(MyJavaFXApplication.class);
      builder.headless(false); // Needed for TestFX
      context = builder.run(getParameters().getRaw().stream().toArray(String[]::new));
  
      FXMLLoader loader = new FXMLLoader(getClass().getResource("main.fxml"));
      loader.setControllerFactory(context::getBean);
      rootNode = loader.load();
  }
  </code></pre>
  
  <p>I now want to test this JavaFX application, for this I use:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.NONE)
  public class MyJavaFXApplicationUITest extends TestFXBase {
  
      @MockBean
      private MachineService machineService;
  
      @Test
      public void test() throws InterruptedException {
          WaitForAsyncUtils.waitForFxEvents();
          verifyThat("#statusText", (Text text ) -&gt; text.getText().equals("Machine stopped"));
          clickOn("#startMachineButton");
          verifyThat("#startMachineButton", Node::isDisabled);
          verifyThat("#statusText", (Text text ) -&gt; text.getText().equals("Machine started"));
      }
  }
  </code></pre>
  
  <p>This starts a Spring context and replaces the "normal" beans with the mock beans as expected. </p>
  
  <p>However, I now get a <code>java.awt.HeadlessException</code> because this 'headless' property is not set to false like is done during normal startup. How to I set this property during the test?</p>
  
  <p>EDIT:</p>
  
  <p>Looking closer it seems that there are 2 context started, one that the Spring testing framework starts and the one I create manually in the <code>init</code> method, so the application under test is not using the mocked beans. If somebody would have a clue how to get the test context reference in the <code>init()</code> method, I would be very happy.</p>