Path: csiph.com!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail From: "Christian H. Kuhn" Newsgroups: de.comp.lang.java Subject: JUnit Test von JButton: Action wird nicht erkannt Date: Thu, 14 Jul 2016 01:09:27 +0200 Lines: 109 Message-ID: Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: individual.net ch59AZYHnzOXmVKoorzH/gOegCFidSgPBejEeTun0V5ZQI2Bc= Cancel-Lock: sha1:Z2saief0XreNRVjuym3heYZbSzU= X-Mozilla-News-Host: snews://news.individual.net:563 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64; rv:45.0) Gecko/20100101 Thunderbird/45.2.0 Xref: csiph.com de.comp.lang.java:12987 Hallo Gemeinde, Ich verzweifle gerade. Vollständiger Code auf https://www.qno.de/gitweb/, Branch v0.8.4. Die wichtigsten Ausschnitte: public class QChessClockJavaAV extends JFrame implements QChessClockObserver, ActionListener { (...) private final transient JButton resetButton; (...) public QChessClockJavaAV(final QChessClock _clock) { super("QChessClock – The JAVA Chess Clock from your FIDE Arbiter"); final JPanel contentPanel = new JPanel(); contentPanel.setLayout(new GridBagLayout()); final GridBagConstraints constraint = new GridBagConstraints(); constraint.fill = GridBagConstraints.BOTH; constraint.weightx = 1; (...) resetButton = new JButton("R"); resetButton.setActionCommand("reset"); resetButton.addActionListener(this); resetButton.setMinimumSize(QChessClockJavaAV.BUTTON_DIMENSION); resetButton.setName("ResetButton"); constraint.gridx = 1; constraint.gridy = 1; contentPanel.add(resetButton, constraint); } (...) public final void actionPerformed(final ActionEvent _event) { final String myActionCommand = _event.getActionCommand(); switch (myActionCommand) { case "reset": clock.resetPressed(); break; (...) default: } } (...) } public class QChessClockJavaAVTest implements QChessClockObserver { private transient QChessClockJavaAV sut; private transient QChessClock clock; private transient JButton resetButton; private transient QChessClockState status; @Before public final void setUp() throws Exception { clock = new QChessClock(); clock.addObserver(this); sut = new QChessClockJavaAV(clock); resetButton = (JButton) TestUtils.getChildNamed(sut, "ResetButton"); } public final void update(final QChessClockState _state) { status = _state; } @Test public final void pressResetButtonTest() throws Throwable { leftButton.doClick(); stopButton.doClick(); Thread.sleep(TWO_CYCLES); resetButton.doClick(); Thread.sleep(TWO_CYCLES); assertSame(QChessClockState.NOT_STARTED, status); } } TestUtils von http://www.javaworld.com/article/2073056/swing-gui-programming/automate-gui-tests-for-swing-applications.html Der Test fails. status ist STOPPED, was es nach stopButton.doClick sein sollte, aber nicht mehr nach resetButton.doClick. resetButton wurde korrekt aufgefunden, wie durch einen Test mit setBackground(Color) herausgefunden wurde. Wird QChessClockJavaAV normal gestartet und mit der Maus bedient, funktioniert der Button wie geplant. Alle anderen Knöpfe reagieren wie gewollt auf doClick(). resetButton.doClick() löst keine Aktion aus, oder zumindest wird sie nicht von actionPerformed() in QChessClockJavaAV aufgefangen. Ein Breakpoint zu Beginn von actionPerformed() hält im Debugger bei allen anderen Knöpfen an, wird aber bei resetButton.onClick() nicht getroffen. Stattdessen wird die nächste Zeile im Test, Thread.sleep(), ausgeführt. Ich habe auch versucht, den Knopf mit doClick(1000) länger zu drücken. Gleicher Fehler. Mir fehlen die Ideen, wo ich noch suchen soll. Alle mir vorstellbaren Fehlermöglichkeiten sind ausgeschlossen. Und am Ende ist es dann wie üblich irgendwas ganz primitives ... TIA QNo