TestFX¶
JavaFX-käyttöliittymiä voi yksikkötestata TestFX testikehyksellä.
Materiaalia siitä, miten TestFX:ää käytetään, löydät vaiheittain:
TestFX:n dokumentaatio on ikävä kyllä varsin huonoa. Rajapintadokumentaatiota voi tarkastella täällä. Lyhyt esimerkki laskimen testaamisesta TestFX:llä:
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]));
}
}
}
}>
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]));
}
}
}
}