英文:
AssertJ Swing occasional test failure
问题
Context
我遇到了一个令人头痛的 AssertJ-Swing (3.9.2) 测试,有时会失败,报告整个组件层次结构为空的 ComponentLookupError。这是 这个问题的确切情况,另外还有一个补充,我在 Travis 实例上使用了 VNC。
有趣的部分是:这些失败只集中在这个特定的测试上,它位于两个测试类的一个类中,这些测试类用于 JDialog 实现的抽象类。
- TodoDialog:抽象类
- NewTodoDialog 和 EditTodoDialog 实现了 TodoDialog。它们执行非常相似的工作,因此有了抽象类。我开始在
TodoDialog
中的initFrame
方法中组装 UI,但后来我把它删除了,并在实现类的构造函数中复制了代码,因为我猜测这可能是测试行为的原因。
受指责的测试
奇怪的行为显示在 NewTodoDialogTest.testClearTagsShouldRestoreTagLabel
上,NewTodoDialogTest 其他测试都正常,没有任何问题。
[ERROR] Tests run: 6, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.404 s <<< FAILURE! - in com.riccardomalavolti.apps.todolist.view.NewTodoDialogTest
[ERROR] testClearTagsShouldRestoreTagLabel(com.riccardomalavolti.apps.todolist.view.NewTodoDialogTest) Time elapsed: 0.727 s <<< ERROR!
org.assertj.swing.exception.ComponentLookupException:
Unable to find component using matcher org.assertj.swing.core.NameMatcher[name='tagComboBox', type=javax.swing.JComboBox, requireShowing=true].
Component hierarchy:
com.riccardomalavolti.apps.todolist.view.NewTodoDialog[name='dialog1', title='New To Do ', enabled=true, modal=false, visible=true, showing=true]
at com.riccardomalavolti.apps.todolist.view.NewTodoDialogTest.testClearTagsShouldRestoreTagLabel(NewTodoDialogTest.java:146)
这是测试的代码:
@Test @GUITest
public void testClearTagsShouldRestoreTagLabel() {
// 选择一个元素
window.comboBox("tagComboBox").selectItem(0);
assertThat(window.comboBox("tagComboBox").selectedItem()).isEqualTo("Bar");
assertThat(window.label("tagLabel").text()).isEqualTo("(Bar)");
// 点击清除标签
window.button("clearButton").click();
assertThat(window.label("tagLabel").text()).isEqualTo(NewTodoDialog.TAG_LBL_NO_TAG_TEXT);
assertThat(window.comboBox("tagComboBox").selectedItem()).isNull();
}
我尝试过使用 Awatility,设置一个等待面板/标签/按钮,但这没有起作用,对我来说这是有道理的,因为其他测试(与相同组件交互)都正常。我还尝试在我的本地机器上使用脚本在 VNC 上执行测试,但它卡在了 KDE 上。
有人有什么想法吗?这是一个非常棘手的问题。
英文:
Context
I got an AssertJ-Swing (3.9.2) test giving me headaches, sometimes it fails with a ComponentLookupError declaring that the entire component hierarchy is empty. This is the exact scenario of this question, with the addition that I'm using VNC on my Travis istance.
The interesting part: failures are concentrated only on this particular test, hosted in a class of two test classes for JDialog implementations of an abstract class.
- TodoDialog: abstract class
- NewTodoDialog and EditTodoDialog implements TodoDialog. They do a very similar job, hence the abstract class. I started assembling the UI in a
initFrame
method inTodoDialog
, but then I erased it and replicate the code in both the constructors of the implementated classes since I guessed could be the cause of the test behavior.
The incriminated test
The strange behavior is shown on
NewTodoDialogTest.testClearTagsShouldRestoreTagLabel
, NewTodoDialogTest other tests are doing fine, no problems at all.
[ERROR] Tests run: 6, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 4.404 s <<< FAILURE! - in com.riccardomalavolti.apps.todolist.view.NewTodoDialogTest
[ERROR] testClearTagsShouldRestoreTagLabel(com.riccardomalavolti.apps.todolist.view.NewTodoDialogTest) Time elapsed: 0.727 s <<< ERROR!
org.assertj.swing.exception.ComponentLookupException:
Unable to find component using matcher org.assertj.swing.core.NameMatcher[name='tagComboBox', type=javax.swing.JComboBox, requireShowing=true].
Component hierarchy:
com.riccardomalavolti.apps.todolist.view.NewTodoDialog[name='dialog1', title='New To Do ', enabled=true, modal=false, visible=true, showing=true]
at com.riccardomalavolti.apps.todolist.view.NewTodoDialogTest.testClearTagsShouldRestoreTagLabel(NewTodoDialogTest.java:146)
That's the test's code:
@Test @GUITest
public void testClearTagsShouldRestoreTagLabel() {
// Selecting an element
window.comboBox("tagComboBox").selectItem(0);
assertThat(window.comboBox("tagComboBox").selectedItem()).isEqualTo("Bar");
assertThat(window.label("tagLabel").text()).isEqualTo("(Bar)");
// Click on Clear Tag
window.button("clearButton").click();
assertThat(window.label("tagLabel").text()).isEqualTo(NewTodoDialog.TAG_LBL_NO_TAG_TEXT);
assertThat(window.comboBox("tagComboBox").selectedItem()).isNull();
}
I've tried to use Awatility, setting an await for the panel/label/button, but that not worked and this have sense to me, since other tests (which interact with the same components) are doing fine. I've tried to use a script to execute the tests on vnc also on my local machine but it frozed KDE.
Anyone has some ideas? It's a very tricky issue.
专注分享java语言的经验与见解,让所有开发者获益!
评论