我完全陷入了java测试;它是通过测试方法将字符“a”发送到 JFrame 组件的 JTextField。
JFrame 类实现 KeyListener 接口,因此覆盖 KeyPressed、KeyTyped 和 KeyReleased。与此同时,我将 JTextField 的所有按键传输到 JFrame;在 JFrame 构造函数内我有:
JTextField txf_version = new JTextField();
txf_version.addKeyListener(this);
我想测试此行为,然后模拟在 JTextField 中键入字符的操作。
我所有的尝试都失败了;我尝试使用 java.awt.Robot 类,如下所示:看看堆栈溢出中的另一篇文章 https://stackoverflow.com/questions/7745959/how-to-simulate-keyboard-presses-in-java,但我得到了一个奇怪的行为:打电话
robot.keyPress(KeyEvent.VK_A);
直接在我的IDE中显示角色,而不是在虚拟JFrame中!尝试使用 requestFocus() 或 requestFocusInWindow() 无效。
我也尝试过 KeyEvents:
KeyEvent key = new KeyEvent(bookWindow.txf_version, KeyEvent.KEY_PRESSED, System
.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, 'a');
bookWindow.txf_version.dispatchEvent(key);
但文本字段的文本属性再次没有改变......
这是我现在的方法:
@Test
void testBtnSaveChangesBecomesRedWhenVersionChanged() throws AWTException,
InterruptedException, NoSuchFieldException, IllegalAccessException {
initTest();
KeyEvent key = new KeyEvent(bookWindow.txf_version, KeyEvent.KEY_PRESSED, System
.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, 'a');
bookWindow.txf_version.dispatchEvent(key);
System.out.println("dans txf_version : " + bookWindow.txf_version.getText
());
assertEquals(Color.RED, bookWindow.getBtnSaveChangesForegroundColor());
}
我可以通过在 JFrame 的子类中编写 main() 方法来查看实际行为,但我认为了解如何模拟 swing 组件测试的按键很有用。
谢谢
编辑:
我根据AJNeufeld的答案更改了测试代码,但仍然不起作用。这是我的测试代码:
@Test
void testBtnSaveChangesBecomesRedWhenVersionChanged() throws AWTException,
InterruptedException, NoSuchFieldException, IllegalAccessException,
InvocationTargetException {
//bookEditor2 & bookWindow
SwingUtilities.invokeAndWait(() -> {
bookWindow = new BookWindow();
VectorPerso two = new VectorPerso();
two.add(le_livre_de_la_jungle);
two.add(elogeMaths);
bookWindow.setTableDatas(two);
bookWindow.table.setRowSelectionInterval(1, 1);
bookWindow.txf_version.requestFocusInWindow();
KeyEvent key = new KeyEvent(bookWindow.txf_version, KeyEvent.KEY_TYPED, System
.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, 'a');
bookWindow.txf_version.dispatchEvent(key);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
System.out.println("dans txf_version : " + bookWindow.txf_version.getText
());
assertEquals(Color.RED, bookWindow.getBtnSaveChangesForegroundColor());
});
}
plintln 行在控制台中生成一个文本:“dans txf_version : 0”,这表示密钥未发送到 txf_version。
EDIT 2:
new try:
@Test
void testBtnSaveChangesBecomesRedWhenVersionChanged() throws AWTException,
InterruptedException, NoSuchFieldException, IllegalAccessException,
InvocationTargetException {
//bookEditor2 & bookWindow
SwingUtilities.invokeAndWait(() -> {
bookWindow = new BookWindow();
VectorPerso two = new VectorPerso();
two.add(le_livre_de_la_jungle);
two.add(elogeMaths);
bookWindow.setTableDatas(two);
bookWindow.table.setRowSelectionInterval(1, 1);
bookWindow.txf_version.requestFocusInWindow();
KeyEvent key = new KeyEvent(bookWindow.txf_version, KeyEvent.KEY_TYPED, System
.currentTimeMillis(), 0, KeyEvent.VK_UNDEFINED, 'a');
bookWindow.txf_version.dispatchEvent(key);
});
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
SwingUtilities.invokeAndWait(() -> {
System.out.println("dans txf_version : " + bookWindow.txf_version.getText
());
assertEquals(Color.RED, bookWindow.getBtnSaveChangesForegroundColor());
});
}