我使用 Assertj Swing(版本 3.17.0)进行的 GUI 单元测试都在本地通过,但有时在 CI 服务器中失败。如果我重试足够长的时间,最终测试套件会变成绿色。我很难弄清楚如何修复这些测试。
我使用 Java 8 和 Github Actions 作为 CI。正如类似问题中所建议的,我使用 VNC 在 CI 上无头运行这些测试,命令如下:
./execute-on-vnc.sh mvn -B -f pom.xml clean verify
Run Code Online (Sandbox Code Playgroud)
这是失败的测试示例(我正在模拟控制器,只是验证单击按钮实际上会使用正确的参数调用控制器方法):
@Mock private ItemController controller;
@Test @GUITest
public void testAddItemButtonShouldDelegateToControllerAddItem() {
window.textBox("itemIdTextField").enterText("1");
window.textBox("itemNameTextField").enterText("Some Item");
window.button(JButtonMatcher.withName("addItemButton")).click();
verify(controller).addItem(new Item("1", "Some Item"));
}
Run Code Online (Sandbox Code Playgroud)
下面是处理 JButton 上的点击的非常直接的代码:
addItemButton.addActionListener(e ->
controller.addItem(new Item(itemIdTextField.getText(), itemNameTextField.getText()))
);
Run Code Online (Sandbox Code Playgroud)
测试每次都在本地通过,没有任何问题,但在 CI 上经常失败,并出现以下错误:
testAddItemButtonShouldDelegateToControllerAddItem(com.example.view.swing.ItemSwingViewTest)
Time elapsed: 0.985 sec <<< FAILURE!
Wanted but not invoked:
controller.addItem(
Item{id='1', name='Some Item'}
);
Actually, there were …Run Code Online (Sandbox Code Playgroud) 我有一组随机排序的字符串(应用程序版本),如下所示:
var arrayOfStrings = ["2.12.5", "2.12.10", "2.2", "2.11.8"]
Run Code Online (Sandbox Code Playgroud)
排序后的数组应该是 ["2.2", "2.12.10", "2.12.5", "2.11.8"] (按最近排序)。我正在尝试对其进行排序。
编辑:糟糕,排序后的数组实际上应该是 ["2.12.10", "2.12.5", "2.11.8", "2.2"]。使用上面的解决方案。
//Compare by string
array.sortInPlace({ //This returns ["2.2", "2.12.5", "2.12.10", "2.11.8"]
$0 > $1
})
//Compare by int
array.sortInPlace({ //This returns ["2.12.10", "2.12.5", "2.11.8", "2.2"]
Int($0.stringByReplacingOccurrencesOfString(".", withString: "")) > Int($1.stringByReplacingOccurrencesOfString(".", withString: ""))
})
Run Code Online (Sandbox Code Playgroud)
这些都不能正常工作。返回正确数组的最佳方法是什么?