我正在使用带有 JavaFX 的 Spring Boot 进行测试(基于一些解释这一点的优秀 YouTube 视频)。
为了使它与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();
}
Run Code Online (Sandbox Code Playgroud)
我现在想测试这个 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", …Run Code Online (Sandbox Code Playgroud) 我有一个春季启动应用程序,我想无头运行它.当我从终端运行时,这是我正在使用的命令:
java -jar myapp.jar --spring.main.headless = true
它是否正确?任何帮助都非常感谢.