首先准备可以使用特定数据填充 TreeView 的应用程序(或更改您当前的应用程序以支持测试模式)。将为 TreeView 提供假数据的实体将是特定数据的模拟对象。从 TreeView 的角度来看,它应该看起来像一个服务,这意味着您需要将服务类中的常用方法提取到接口中,并使 TreeView 使用该接口而不是具体类。
要处理 UI 测试,您可以使用JemmyFX库。您可以创建简单的测试来验证您的 TestView 的任何 UI 属性或模仿鼠标单击或文本输入等用户操作。您可能不用担心线程问题,jemmy 会为您处理。
它可以看起来下一个方式(我在这里使用junit作为测试工具):
public class TreeTest {
@BeforeClass
public static void setUpClass() throws Exception {
// running your specially crafted FX application with mock service data
// you can do it any way, e.g. by calling main() method with some parameters
AppExecutor.executeNoBlock(TreeApp.class);
}
@Test // this is junit annotation for test
public void fooTest() {
// here we are receiving special jemmyfx entity which knows how to handle
// tree views and is attached to your TreeView
// (if you app has only one TreeView, you may need additional logic for several ones)
TreeViewDock tree = new TreeViewDock(new SceneDock().asParent());
// this way we can find some item which you expected to see in you TreeView
// because you've created you mock data this way.
// Note that underlying code will respect UI threading,
// will understand that UI can have delay and will give it certain time without blocking
// and will throw descriptive exception in case expected item wasn't found
tree.asTree().selector().select(new ByToStringLookup("item1"));
// you can find subitems
tree.asTree().selector().select(new ByToStringLookup("item1"), new ByToStringLookup("subitem1");
// and perform operations on them, e.g. expand:
new TreeItemDock(tree.asItemParent(), new EqualsLookup("item2")).asTreeItem().expand();
//etc
}
您可以通过代码完成提示在网站上找到更多信息