1
我有這個在我的主類Board
(擴展JPanel
):如何在junit測試中模擬按鍵?
public class TAdapter extends KeyAdapter {
public void keyPressed(KeyEvent e) {
int key = e.getKeyCode();
if ((key == KeyEvent.VK_LEFT) && (!right)) {
left = true;
up = false;
down = false;
}
}
我想測試,如果當我按下左鍵左側爲真:
@Test
public void testKeyPressed() throws AWTException {
Board instance = new Board();
Robot rob = new Robot();
instance.setFocusable(true);
instance.requestFocus();
rob.keyPress(KeyEvent.VK_LEFT);
System.out.println(instance.up);
}
謝謝!
它看起來對我來說,那類'擴展KeyAdapter'。 –
Board擴展Jpanel; TAdapter擴展了KeyAdapter; TAdapter是在Board類中定義的。 –
'rob.keyPress(...)'如何連接到'TAdapter'的'keyPressed(...)'方法?但是,您可以直接在單元測試中調用'instance.getTAdapter()。keyPressed(...)'(如果有類似'getTAdapter()')的內容並驗證結果。 –