【问题标题】:How to use hamcrest matchers in Testng assertEquals?如何在 Testng assertEquals 中使用 hamcrest 匹配器?
【发布时间】:2018-11-22 12:37:13
【问题描述】:

我目前正在使用 testng assertEquals 和 softassertion,不,我有一个验证场景,实际是一个完整的 String,而预期是一个子字符串。

代码

assertThat("This is actual string", containsString("expected"));

错误信息

java.lang.NoSuchMethodError:
org.hamcrest.Matcher.describeMismatch(Ljava/lang/Object;Lorg/hamcrest/Description;)V

代码

softAssert.assertEquals("This is actual string", containsString("actual"));

错误信息

java.lang.AssertionError:
以下断言失败:预期 [包含“实际”的字符串] 但找到 [这是实际字符串] 预期:包含“实际”的字符串实际:这是实际字符串

【问题讨论】:

    标签: java unit-testing testing testng hamcrest


    【解决方案1】:

    你不能用 assertEquals 来做,你可以用 assertTrue 来做。使用下面的代码,

    String actual="This is actual string";
    assertTrue(actual.contains("actual"));
    

    【讨论】:

    • 感谢输入,我也试过了,如果断言失败,断言错误说预期为真,实​​际为假。一般断言错误消息将类似于 Expected:actual Actual:This is actual string
    • @BharathanKumaran 无法理解该评论。你的问题解决了吗?
    • 我已经在使用上面那个了,上面那个的缺点是如果包含失败,断言错误日志会说预期为真,实​​际为假,hamcrest断言会说预期“预期string" actual 这是实际的字符串,
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-08-16
    • 1970-01-01
    • 1970-01-01
    • 2018-10-07
    • 1970-01-01
    • 1970-01-01
    • 2014-04-16
    相关资源
    最近更新 更多