1

我有一个简单的应用程序,如果用户单击登录按钮,则会将一条消息打印到标准输出。我正在尝试开始使用 testfx,但是当它模拟按钮单击时,不会打印任何消息。这是预期的行为,还是我缺少什么?

DialogioController.java

package dialogio;

import java.net.URL;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.ListView;
import javafx.scene.control.TabPane;
import javafx.scene.control.TextField;
import javafx.scene.input.MouseEvent;
import javafx.scene.control.Button;

public class DialogioController {

    @FXML
    private ResourceBundle resources;

    @FXML
    private URL location;

    @FXML
    private ListView<?> peopleList;

    @FXML
    private TabPane tabList;

    @FXML
    private TextField usernameField;

    @FXML
    private TextField passwordField;

    @FXML
    private TextField ipField;

    @FXML
    private Button loginButton;

    @FXML
    void login(MouseEvent e) {
        System.out.println("Trying to login!");
    }


    @FXML
    void initialize() {
        assert peopleList != null : "fx:id=\"peopleList\" was not injected: check your FXML file 'Dialogio.fxml'.";
        assert tabList != null : "fx:id=\"tabList\" was not injected: check your FXML file 'Dialogio.fxml'.";
        assert usernameField != null : "fx:id=\"usernameField\" was not injected: check your FXML file 'Dialogio.fxml'.";
        assert passwordField != null : "fx:id=\"passwordField\" was not injected: check your FXML file 'Dialogio.fxml'.";
        assert ipField != null : "fx:id=\"ipField\" was not injected: check your FXML file 'Dialogio.fxml'.";

    }
}

DialogioTest.java

import org.loadui.testfx.GuiTest;
import java.io.IOException;
import java.net.URL;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;
import org.junit.Test;
import javafx.scene.control.Button;
import javafx.scene.control.ToggleButton;
import javafx.scene.control.TextField;

public class DialogioTest extends GuiTest{
    @Override
    protected Parent getRootNode() {
        Parent parent = null;
        try {
            parent = FXMLLoader.load(getClass().getResource("Dialogio.fxml"));
            System.out.println("Loaded parent");
            return parent;
        } catch (IOException exc) {
            exc.printStackTrace();
        }
        return parent;
    }   
    @Test
    public void clickLogin(){
        Button loginButton = find("#loginButton");
        click(loginButton);
    }
}

Dialogio.java

package dialogio;
import javafx.application.Application;
import java.net.URL;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.Parent;
import javafx.fxml.FXMLLoader;

public class Dialogio extends Application{
    public static void main(String[] args){
        launch();
    }
    public void start(Stage primaryStage){
        primaryStage.setTitle("Dialogio");
        Parent root = null;
        String sceneFile = "Dialogio.fxml";
        URL url = null;
        try{
            url = getClass().getClassLoader().getResource(sceneFile);
            root = FXMLLoader.load(url);
            Scene scene = new Scene(root);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
        catch(Exception exc){
            exc.printStackTrace();
        }
    }
}

Dialogio.fxml

<?xml version="1.0" encoding="UTF-8"?>

<?import javafx.scene.control.Button?>
<?import javafx.scene.control.ListView?>
<?import javafx.scene.control.Tab?>
<?import javafx.scene.control.TabPane?>
<?import javafx.scene.control.TextField?>
<?import javafx.scene.layout.AnchorPane?>
<?import javafx.scene.layout.ColumnConstraints?>
<?import javafx.scene.layout.GridPane?>
<?import javafx.scene.layout.RowConstraints?>


<AnchorPane maxHeight="-Infinity" maxWidth="-Infinity" minHeight="-Infinity" minWidth="-Infinity" prefHeight="400.0" prefWidth="600.0" xmlns="http://javafx.com/javafx/8.0.76-ea" xmlns:fx="http://javafx.com/fxml/1" fx:controller="dialogio.DialogioController">
   <children>
      <GridPane layoutX="14.0" layoutY="66.0" prefHeight="400.0" prefWidth="600.0" AnchorPane.bottomAnchor="0.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0" AnchorPane.topAnchor="0.0">
        <columnConstraints>
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
          <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
            <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
        </columnConstraints>
        <rowConstraints>
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
          <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
        </rowConstraints>
         <children>
            <ListView fx:id="peopleList" prefHeight="200.0" prefWidth="200.0" GridPane.rowSpan="3" />
            <TabPane fx:id="tabList" prefHeight="134.0" prefWidth="353.0" tabClosingPolicy="UNAVAILABLE" GridPane.columnIndex="1" GridPane.columnSpan="3" GridPane.rowSpan="3">
               <tabs>
                  <Tab text="Home">
                    <content>
                      <AnchorPane minHeight="0.0" minWidth="0.0" prefHeight="180.0" prefWidth="200.0">
                           <children>
                              <GridPane layoutY="94.0" AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0">
                                <columnConstraints>
                                  <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" />
                                </columnConstraints>
                                <rowConstraints>
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                  <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                    <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" />
                                </rowConstraints>
                                 <children>
                                    <TextField fx:id="usernameField" promptText="username" />
                                    <TextField fx:id="passwordField" promptText="password" GridPane.rowIndex="1" />
                                    <TextField fx:id="ipField" promptText="server ip" GridPane.rowIndex="2" />
                                    <Button fx:id="loginButton" mnemonicParsing="false" onMouseClicked="#login" text="Login" textAlignment="CENTER" GridPane.rowIndex="3" />
                                 </children>
                              </GridPane>
                           </children>
                        </AnchorPane>
                    </content>
                  </Tab>
               </tabs>
            </TabPane>
         </children>
      </GridPane>
   </children>
</AnchorPane>
4

1 回答 1

1

我只需要添加

test {
  testLogging.showStandardStreams = true
}

对我build.gradle

于 2016-04-12T17:05:26.270 回答