I have a JavaFx class that creates a scene that's the main menu of a game I'm creating and all this scene has is four buttons each leading to different scenes. For example, if the user clicks on the "How to play" button, it leads them to another scene that shows them how to play. This class has one method which returns the start menu scene.
This class also doesn't extend the application class as the main class is the one that extends it and sets the primary stage's scene to the start menu scene returned by the main menu class' method.
I have been trying to figure out a way to automate test this start menu class alone but all tutorials I have found so far test classes that extend the Application class. What I want to find is a way to automate test classes separately that don't extend the Application class. For example, in the case of this class, I want to test that the buttons are working or that the buttons have been added to the scene.
Here's the code to help make my explanation more clear:
public class StartScreen {
private Scene helpScene;
private Scene levels;
private Scene endScene;
private Scene startScene;
/**
* Starts the game by displaying the main menu.
*
* @param primaryStage the primary stage of the game
* @return the main menu scene
* @throws IOException in case an error occurs when showing the EndScreen after the "view high scores" button is pressed
*/
public Scene startGame(Stage primaryStage) throws IOException {
MyStage helpScreen = new MyStage();
helpScene = new Scene(helpScreen,500,600);
BackgroundImage backgroundImage1 = new BackgroundImage(new Image("file:src/p4_group_8_repo/images/helpBackground.png",500,600, false, true),
BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
helpScreen.setBackground(new Background(backgroundImage1));
Button startButton = new Button("Start game");
startButton.setOnAction(actionEvent -> {
LevelsMenu levelsMenu = new LevelsMenu();
levels = levelsMenu.levelsMenu(primaryStage);
primaryStage.setScene(levels);
});
Button helpButton = new Button("How to play the game");
helpButton.setOnAction(actionEvent -> primaryStage.setScene(helpScene));
Button exitButton = new Button("Exit game");
exitButton.setOnAction(actionEvent -> System.exit(1));
endScene = new EndScreen().start(primaryStage);
Button highScoreButton = new Button("View High-scores");
highScoreButton.setOnAction(actionEvent -> {
try {
primaryStage.setScene(endScene);
} catch (Exception e) {
System.out.println(e.getMessage());
}
});
VBox vBox = new VBox();
buttons(vBox, startButton);
buttons(vBox, helpButton);
buttons(vBox, exitButton);
buttons(vBox, highScoreButton);
vBox.getChildren().addAll(startButton,helpButton,highScoreButton,exitButton);
StackPane stackPane = new StackPane();
stackPane.getChildren().add(vBox);
startScene = new Scene(stackPane,500,600);
BackgroundImage backgroundImage2 = new BackgroundImage(new Image("file:src/p4_group_8_repo/images/FroggerBG2.png",500,600, false, true),
BackgroundRepeat.REPEAT, BackgroundRepeat.NO_REPEAT, BackgroundPosition.DEFAULT, BackgroundSize.DEFAULT);
vBox.setBackground(new Background(backgroundImage2));
ToggleButton backButton = new ToggleButton("Back");
helpScreen.getChildren().add(backButton);
backButton.setOnAction(event3 -> {
if(backButton.isSelected())
primaryStage.setScene(startScene);
});
return startScene;
}
Main class:
public class Main extends Application {
/**
* Main method. The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) {
launch(args);
}
/**
* {@inheritDoc}
* Starts the game application.
*
* @param primaryStage the primary stage of the game.
* @throws Exception throws an exception as the StartScreen method throws an exception.
*/
@Override
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Frogger");
primaryStage.setScene(new StartScreen().startGame(primaryStage));
primaryStage.show();
}
}
Any help would be greatly appreciated.
Edit: I forgot to mention that the type of testing I am looking for is automating testing like using JUnit or TestFX.
Aucun commentaire:
Enregistrer un commentaire