mercredi 18 octobre 2017

Testing for JavaFX

I'm trying to write tests for my JavaFX project, but I still don't get it enough.

I have for example LoginController class and I would like to check if login process will be correct and if after press button "Load" the application will give me next side. There is my class:

public class LoginController implements Initializable {

@FXML private TextField login;
@FXML private PasswordField password;
@FXML private Label isConnected;
@FXML private Button load;
@FXML private Button createNewProfile;

public LoginModel loginModel = new LoginModel();

@FXML   
public void loadA(ActionEvent event) {
    try {
        if (loginModel.isLogin(login.getText(), password.getText())) {
            isConnected.setText("Login and Password is correct");

            ((Node)event.getSource()).getScene().getWindow().hide();
            Stage primaryStage5 = new Stage();
            FXMLLoader loader = new FXMLLoader();
            Pane root5 = loader.load(getClass().getResource("/application/Main.fxml").openStream());
            MainController mainController = (MainController)loader.getController();
            mainController.getUserName(loginModel.getUser());
            Scene scene5 = new Scene(root5);
            scene5.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage5.setScene(scene5);
            primaryStage5.setTitle("ListAddRemove");
            primaryStage5.show();
        } else {
            isConnected.setText("Login or Password isn't correct");
        }
    } catch (Exception e) {
            isConnected.setText("Login or Password isn't correct");
    }
}

And there is my test method. When I run this I've got NullPointerException for USERNAME and PASSWORD. Should I do "press simulation for button" and then get value or what?

public class test2 extends Application {

@Override
public void start(Stage primaryStage) {
    try {
        Parent root = FXMLLoader.load(getClass().getResource("/application/Login.fxml"));
        Scene scene = new Scene(root);
        scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
        primaryStage.setScene(scene);
        primaryStage.setTitle("Login");
        primaryStage.show();
    } catch(Exception e) {
        e.printStackTrace();
    }
}

@Before
public void startMain(){
    Main main = new Main();
    launch(main.getClass());
}

@Test
public void logTest() throws IOException{

    LoginController log = new LoginController();

    String USERNAME = log.getLogin().getText();
    String PASSWORD = log.getPassword().getText();

    String login = "log";
    String pass = "pass";

    assertEquals(login, USERNAME);
    assertEquals(pass, PASSWORD);

}

}

Aucun commentaire:

Enregistrer un commentaire