Skip to content

Small JUnit5 Extension to unit test JavaFX views/controllers

License

Notifications You must be signed in to change notification settings

alwins0n/fxtension

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

6 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

FXTension

Small JUnit5 Extension to unit test JavaFX views/controllers

Motivation

This project was created to simplify the unit testing of JavaFX views and controllers.

While there are more complete frameworks like TestFX this project aims to be a lightweight alternative for simple unit tests.

The usecases are

  • check if the view is correctly initialized (components wired up correctly)
  • check if properties are correctly bound
  • check if concurrency is working correctly (being able to mock Tasks)

Usage

An example using @RunFX in a test class extended with FXExtension:

@ExtendWith(FXtension.class)
class FXtensionBasicTest {

    @Test
    void withoutAnnotation_shouldRunWithoutFX() {
        assertFalse(Platform.isFxApplicationThread());
    }

    @Test
    @RunFX
    void withAnnotation_shouldRunInFX() {
        assertTrue(Platform.isFxApplicationThread());
    }

}

An example using @RunFXML:

The view:

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

<?import javafx.scene.control.CheckBox?>
<?import javafx.scene.layout.VBox?>
<VBox xmlns="http://javafx.com/javafx"
      xmlns:fx="http://javafx.com/fxml"
      fx:controller="io.github.alwins0n.fxtension.test.MyController"><!-- instance of this must be a field in the test -->
    <CheckBox fx:id="check"/>
</VBox>

The test:

@ExtendWith(FXtension.class)
class FXtensionViewTest {

    MyController unit; // the controller used by the FXMLLoader
    // BuilderFactory factory; // we could inject other objects for the FXMLLoader here
    MyModel model;

    @BeforeEach
    void setUp() {
        model = new MyModel();
        unit = new MyController(model);
    }

    @Test
    @RunFXML("views/test.fxml")
    void runView_shouldUpdateModel() {
        assertFalse(model.checkProperty().get());
        unit.check.setSelected(true);
        assertTrue(model.checkProperty().get());
    }

}

About

Small JUnit5 Extension to unit test JavaFX views/controllers

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages