是否可以与testfx同时测试2个Javafx窗口?

时间:2018-09-12 14:27:09

标签: unit-testing javafx javafx-8 testfx

我正在使用java,javafx,testfx和Java API的websockets创建聊天项目。 我的测试运行良好,但是我创建了一个困扰我的新测试。这项新测试将涉及服务器与客户端之间的通信实现。无论如何,我没有向您解释该协议,问题不存在。问题是我需要创建和启动一个应用程序两次:第一个用于第一个应用程序(将是服务器),第二个将作为客户端。

但是testfx似乎旨在同时测试一个javafx窗口,并且使用诸如“ find(String)”之类的某些方法,其行为就像是实际的窗口一样。这就是我所了解的。 我将向您展示我的代码(由于我同时尝试启动2个窗口而被破坏了),但是在此之前,我想要的是:我创建了两个ApplicationRunner类(ApplicationRunner的目的是管理应用程序,包括“ main”方法),每个包含其FenetreMig变量(=窗口)。例如,测试会要求apr.fenetre.lbl_state.getGraphic()以获得包含在其中的窗口的Label lbl_state的图片名为apr的ApplicationRunner实例。

这是我的测试类的(混乱)代码(如有需要,请随时询问我更多信息):

package lorry;

import javafx.application.Platform;
import javafx.scene.Node;
import javafx.scene.control.Label;
import lorry.GraphicUtilities.FenetreMig;
import org.junit.jupiter.api.*;
import org.mockito.MockitoAnnotations;
import org.testfx.api.FxRobot;
import org.testfx.api.FxToolkit;

import java.util.concurrent.TimeoutException;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.testfx.api.FxAssert.verifyThat;
import static org.testfx.util.NodeQueryUtils.hasText;

//@ExtendWith(MockitoExtension.class)
public class unitTestsTry2 {

    private static final String LOCAL_IP = "127.0.0.1";
    private static final String REMOTE_IP = "127.0.0.1";
    private static final String LOCAL_PORT = "1001";
    private static final String REMOTE_PORT = "1002";
    static ApplicationRunner apr;





    @BeforeAll
    static void before() throws Exception {
        FxToolkit.registerPrimaryStage();
        //getApplicationRunnerWithGUI();
        MockitoAnnotations.initMocks(unitTestsTry2.class);


    }

    private ApplicationRunner getApplicationRunnerWithGUI(String LO_IP, String LO_PORT, String RE_IP, String RE_PORT)  {
        ApplicationRunner local_apr = getApplicationRunner(LO_IP, LO_PORT, RE_IP, RE_PORT);
        try {
            FxToolkit.setupApplication(FenetreMig.class);

        } catch (TimeoutException e) {
            e.printStackTrace();
        }
        //the following call waits for the constructor to be called and the window's variables to be filled
        local_apr.fenetre = FenetreMig.waitForFenetreMig();

        //new Thread(apr.fenetre).start();
        //testWindow.waitUntilInjections();
        local_apr.fenetre.lbl_state = find("#lbl_state");
        local_apr.fenetre.txf_insert = find("#txf_insert");
        local_apr.fenetre.txa_lines = find("#txa_lines");
        local_apr.fenetre.btn_quit = find("#btn_quit");
        local_apr.fenetre.btn_send = find("#btn_send");


        return local_apr;
    }

    private ApplicationRunner getApplicationRunner(String LO_IP, String LO_PORT, String RE_IP, String RE_PORT) {
        return ApplicationRunner.createAndInitializeExceptWindow(new String[]{LO_IP,LO_PORT,RE_IP,RE_PORT});
    }

    @Test
    void oneTest() {
        apr = getApplicationRunnerWithGUI(LOCAL_IP,LOCAL_PORT,REMOTE_IP,REMOTE_PORT);
        //verifyThat("#btn_send", hasText("Envoyer"));
        Platform.runLater(() -> assertEquals(apr.fenetre.btn_send.getText(), "Envoyer"));

    }

    @Test
    void end2_startupAloneStateRedThenYellow() {
        apr=getApplicationRunnerWithGUI(LOCAL_IP,LOCAL_PORT,REMOTE_IP,REMOTE_PORT);

        apr.tryToConnect();

        assertEquals(((Label) find("#lbl_state")).getGraphic(), FenetreMig.WAITING);

    }

    @Test
    void StartupClientTriesAndSucceedsToConnect() { <--------
        ApplicationRunner app_server = getApplicationRunnerWithGUI(LOCAL_IP, LOCAL_PORT, REMOTE_IP, REMOTE_PORT);
        app_server.tryToConnect();
        ApplicationRunner app_client = getApplicationRunnerWithGUI(REMOTE_IP, REMOTE_PORT, LOCAL_IP, LOCAL_PORT);
        app_client.tryToConnect();

        Platform.runLater(() -> assertEquals(app_server.fenetre.lbl_state.getGraphic(), FenetreMig.OK));
        Platform.runLater(() -> assertEquals(app_client.fenetre.lbl_state.getGraphic(), FenetreMig.OK));




    }

    @Disabled
    @Test
    void startupAloneFailsAsClientThenRunServer() {
        apr=getApplicationRunnerWithGUI(LOCAL_IP,LOCAL_PORT,REMOTE_IP,REMOTE_PORT);

        apr.tryToConnect();





    }

    @Disabled
    @Test
    void ApplicationRunnerSessionWhenFilledThenGreen() {


    }

    @Disabled
    @Test
    void ApplicationRunnerSessionIfFilledAndAfterNullThenRedAndBeginConnectionsAsAtStartup() {



    }

    //**********************************************************************************************************
    //**********************************************************************************************************
    //**********************************************************************************************************



    public <T extends Node> T find(final String query) {
        /** TestFX provides many operations to retrieve elements from the loaded GUI. */
        return new FxRobot().lookup(query).query();
    }

    @AfterEach
    void tearDown() {
        if (apr.server!=null) apr.server.stop();


    }

    @AfterAll
    static void tearDownAll() throws TimeoutException {
        FxToolkit.hideStage();
        //release(new KeyCode[]{});
        //release(new MouseButton[]{});
    }
}

我在代码中放入的箭头是新测试。显然,如果可能的话,诸如FxToolkit.setupApplication(FenetreMig.class);之类的语句将消失。您认为可以同时测试2个应用吗?

谢谢

编辑 我已经通过用Mockito模拟窗口类来部分解决了这个问题。

0 个答案:

没有答案