java - 从多个线程向 SoftAssertions 添加失败
问题描述
SoftAssertions
当我需要确保多个条件保持或收集尽可能多的失败时,AssertJ 中的in AssertJ很有用:
final SoftAssertions softly = new SoftAssertions();
softly.assertThat("foo").hasToString("foo");
softly.assertThat("bar").hasToString("bar");
softly.assertAll();
尽管如此,当我需要收集在多个线程中产生的故障时,多线程环境看起来不再那么乐观了。例如,此代码被破坏,因为SoftAssertions
从其他线程对实例所做的更改可能在线程中不可见main
:
final SoftAssertions softly = new SoftAssertions();
final ForkJoinPool forkJoinPool = ForkJoinPool.commonPool();
forkJoinPool.execute(() -> {
softly.assertThat("foo").hasToString("foo");
});
forkJoinPool.execute(() -> {
softly.assertThat("bar").hasToString("bar");
});
softly.assertAll();
解决上述问题的最佳方法是什么?
在我使用它们的任何地方都使用同步块进行保护SoftAssertions
似乎不是一个好的解决方案(尤其是从测试可读性的角度来看)。
解决方案
推荐阅读
- latex - 在r markdown表中打印带有换行符的长字符值(到latex pdf)
- ios - 在 Swift 的谷歌地图中通过 Lat Long 获取地点 ID
- angular - Angular7 SSR、webpack 和 splitChunks
- php - PHP中来自TXT的回显字符串
- javascript - 如何等待变量具有值以便我可以执行其他代码?
- java - 始终以编程方式在同一窗口中打开 ImageJ 中的新图像(java)
- json - 如何在 JSON 路径中转义 @ 符号
- javascript - 在 javascript 中导入:意外令牌
- javascript - 导航 401 Http 失败
- css - 样式化组件中的驼峰式 CSS 或常规 React 的情感?