电话
400 9058 355
本文介绍在 junit 5 中如何可靠地断言异常消息中包含若干无序字符串(如 "b, c, d"),避免因 `hashset` 迭代顺序不确定导致测试偶然失败。核心方案包括:使用 `linkedhashset` 控制顺序,或对捕获的异常消息进行结构化解析与子串/内容校验。
在单元测试中验证异常消息时,若消息内容依赖于 HashSet 等无序集合的遍历结果(例如 setA.stream().filter(...).collect(...)),其元素输出顺序不可预测,将直接导致基于完整字符串匹配的断言(如 assertThrows(..., "The strings b, c, d are..."))间歇性失败——这并非测试缺陷,而是设计隐患。
修改被测代码的测试调用方式,传入有序集合替代 HashSet。例如,在测试中使用 LinkedHashSet 或 TreeSet:
@Test
void funcSubSet_throwsWithPredictableOrder() {
final Set setA = new LinkedHashSet<>(Arrays.asList("a", "b", "c", "d")); // 保持插入顺序
final Set setB = new LinkedHashSet<>(Arrays.asList("a"));
// 通过依赖注入或重构使 funcSubSet 接收参数,而非硬编码
Exception exception = assertThrows(IllegalArgumentExcepti
on.class,
() -> funcSubSet(setA, setB));
assertEquals("The strings b, c, d are present in setA but not in setB",
exception.getMessage());
} ? 提示:将集合作为参数传入 funcSubSet(Set setA, Set setB) 不仅提升测试可控性,也显著增强方法内聚性与可复用性,符合“易测即易用”原则。
当无法调整被测逻辑(如遗留系统)时,应避免强依赖完整消息字符串,转而校验消息结构 + 关键内容:
@Test
void funcSubSet_throwsWithFlexibleMessage() {
Exception exception = assertThrows(IllegalArgumentException.class,
() -> funcSubSet());
String msg = exception.getMessage();
// 1. 校验固定前缀与后缀
assertTrue(msg.startsWith("The strings "), "Message must start with prefix");
assertTrue(msg.endsWith(" are present in setA but not in setB"),
"Message must end with suffix");
// 2. 提取中间变量部分(去除前后固定文本)
String variablesPart = msg.substring(
"The strings ".length(),
msg.length() - " are present in setA but not in setB".length()
).trim();
// 3. 验证所有预期元素均存在(忽略顺序和分隔符细节)
assertTrue(variablesPart.contains("b"), "Expected 'b' in message");
assertTrue(variablesPart.contains("c"), "Expected 'c' in message");
assertTrue(variablesPart.contains("d"), "Expected 'd' in message");
// 可选:进一步验证是否仅含预期元素(防误报)
Set actualElements = Arrays.stream(variablesPart.split(",\\s*"))
.map(String::trim)
.collect(Collectors.toSet());
assertEquals(Set.of("b", "c", "d"), actualElements);
} 通过以上任一方案,均可彻底消除因集合迭代顺序不确定性引发的测试不稳定性,让异常消息验证既健壮又可读。
邮箱:8955556@qq.com
Q Q:8955556
本文详解如何将Go官方present工具(用于生成HTML5...
PySNMP在不同版本中对SNMP错误状态(errorSta...
time.Sleep仅阻塞当前goroutine,其他gor...
PHPfopen()创建含特殊符号的文件名失败主因是操作系统...
WooCommerce中通过代码为分组产品动态聚合子商品的属...
io.ReadFull返回io.ErrUnexpectedE...
本文详解Yii2中控制器向视图传递ActiveRecord数...
本文详解为何通过wp_set_object_terms()为...
Pytest中使用@mock.patch类装饰器会导致补丁泄...
带缓冲的channel是并发安全的FIFO队列;make(c...