This course has already ended.

TestFXΒΆ

Note

TestFX is extra-material. There are no questions of TestFX in the material and the use of TestFX is optional in the project work.

JavaFX user interfaces can be unit tested with the TestFX framework.

Material on how TestFX is used can be found:

However, we are aware that the documentation of TestFX is poor. The interface documentation can be read here. A small example on how to test the calculator with TestFX:

import org.testfx.framework.junit5.ApplicationTest;

public class CalculatorTest extends ApplicationTest {
    @Override
    public void start(Stage stage) {
        new Calculator().start(stage);
    }

    @Test
    @DisplayName("Testing button basics")
    public void checkButtons() {
        String[] queries = {"#btnAdd", "#btnSub", "#btnMul", "#btnDiv"};
        String[] texts = {"Add", "Subtract", "Multiply", "Divide"};
        for(int i = 0; i < queries.length; i++) {
        Button node = fromAll().lookup(queries[i]).query();
        assertTextsEqual(node.getText(), texts[i], queries[i].substring(1));
        }
    }

    @Test
    @DisplayName("Testing operations")
    public void checkOperations() {
        TextField op1 = fromAll().lookup("#fieldOp1").query();
        TextField op2 = fromAll().lookup("#fieldOp2").query();
        Label res = fromAll().lookup("#fieldRes").query();
        String[] queries = {"#btnAdd", "#btnSub", "#btnMul", "#btnDiv"};
        String[] operations = {"+", "-", "*", "/"};
        String[][] operands = {{"12.5", "62.5"}, {"-900", "30"}};
        String[][] results = {{"75.0", "-50.0", "781.25", "0.2"},
                            {"-870.0", "-930.0", "-27000.0", "-30.0"}};
        for(int i = 0; i < queries.length; i++) {
        Button node = fromAll().lookup(queries[i]).query();
        for(int j = 0; j < operands.length; j++) {
            op1.setText(operands[j][0]);
            op2.setText(operands[j][1]);
            clickOn(node);
            assertTextsEqual(res.getText(), results[j][i], String.format(
                    "fieldRes after computing %s %s %s", operands[j][0],
                    operations[i], operands[j][1]));
        }
        }
    }
}
Posting submission...