我正在使用Spring Boot和JavaFX進行測試(基於解釋這個的some excellent YouTube videos)。如何在Spring Boot測試中設置'無頭'屬性?
,使其與TestFX工作,我需要建立這樣的背景下:
@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();
}
我現在要測試的這款JavaFX應用程序,爲了這個,我使用:
@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) -> text.getText().equals("Machine stopped"));
clickOn("#startMachineButton");
verifyThat("#startMachineButton", Node::isDisabled);
verifyThat("#statusText", (Text text) -> text.getText().equals("Machine started"));
}
}
這將啓動一個Spring上下文並按照預期用模擬bean替換「正常」的bean。
但是,我現在得到一個java.awt.HeadlessException
,因爲這個'headless'屬性沒有像正常啓動時那樣設置爲false。如何在測試期間設置此屬性?
編輯:
更仔細地觀察,似乎有2個方面開始,一個春節測試框架開始和一個我在init
方法手動創建,所以測試的應用程序未使用嘲笑豆。如果有人想知道如何獲得init()
方法中的測試環境參考,我會非常高興。
可能是[link](http://stackoverflow.com/questions/36160353/why-does-swing-think-its-headless-under-spring-boot-but-not-under-spring-or- pl)會幫助你。 –