Skip to content

Commit

Permalink
Add tests for JavaFX
Browse files Browse the repository at this point in the history
  • Loading branch information
aureliony committed Feb 23, 2024
1 parent 526e58c commit 9d9d51a
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ dependencies {
implementation 'me.xdrop:fuzzywuzzy:1.4.0'
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'
testImplementation 'org.mockito:mockito-core:5.10.0'
testImplementation 'org.testfx:testfx-junit5:4.0.18'
testImplementation 'org.assertj:assertj-core:3.25.3'
testImplementation 'org.hamcrest:hamcrest:2.2'
}

java {
Expand Down
66 changes: 66 additions & 0 deletions src/test/java/convobot/gui/JavaFxTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
package gui;

// this code is taken from https://github.com/TestFX/TestFX
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.testfx.api.FxRobot;
import org.testfx.assertions.api.Assertions;
import org.testfx.framework.junit5.ApplicationExtension;
import org.testfx.framework.junit5.Start;

import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

@ExtendWith(ApplicationExtension.class)
class JavaFxTest {

private Button button;

/**
* Will be called with {@code @Before} semantics, i. e. before each test method.
*
* @param stage - Will be injected by the test runner.
*/
@Start
private void start(Stage stage) {
button = new Button("click me!");
button.setId("myButton");
button.setOnAction(actionEvent -> button.setText("clicked!"));
stage.setScene(new Scene(new StackPane(button), 100, 100));
stage.show();
}

/**
* @param robot - Will be injected by the test runner.
*/
@Test
void buttonShouldHaveText(FxRobot robot) {
Assertions.assertThat(button).hasText("click me!");
// or (lookup by css id):
Assertions.assertThat(robot.lookup("#myButton").queryAs(Button.class)).hasText("click me!");
// or (lookup by css class):
Assertions.assertThat(robot.lookup(".button").queryAs(Button.class)).hasText("click me!");
// or (query specific type):
Assertions.assertThat(robot.lookup(".button").queryButton()).hasText("click me!");
}

/**
* @param robot - Will be injected by the test runner.
*/
@Test
void textChangesIfButtonClicked(FxRobot robot) {
// when:
robot.clickOn(".button");

// then:
Assertions.assertThat(button).hasText("clicked!");
// or (lookup by css id):
Assertions.assertThat(robot.lookup("#myButton").queryAs(Button.class)).hasText("clicked!");
// or (lookup by css class):
Assertions.assertThat(robot.lookup(".button").queryAs(Button.class)).hasText("clicked!");
// or (query specific type)
Assertions.assertThat(robot.lookup(".button").queryButton()).hasText("clicked!");
}
}

0 comments on commit 9d9d51a

Please sign in to comment.