【问题标题】:JUnit Assertion error with identical strings具有相同字符串的 JUnit 断言错误
【发布时间】:2020-08-08 07:34:22
【问题描述】:

我正在尝试制作一个测试用例来检查 Dropwizard Metric Meter 名称: 代码如下:

@Test
public void getMeterName(){
  String metricsPrefix = "com.company.team";
  String tagSupplied = "tenant.db.table";
  String expectedMeterName = "com.company.team.tenant.db.table";

  assertSame(expectedMeterName,MetricRegistry.name(metricsPrefix,tagSupplied));
}

这是我得到的错误:

java.lang.AssertionError: expected same:<com.company.team.tenant.db.table> was not:<com.company.team.tenant.db.table>
Expected :com.company.team.tenant.db.table
Actual   :com.company.team.tenant.db.table

我在这里错过了什么?

【问题讨论】:

    标签: java testing junit dropwizard metrics


    【解决方案1】:

    我在这里错过了什么?

    您正在测试的字符串相等但不是同一个对象。

    您在应该使用assertEquals 的地方使用assertSame

    这类似于使用==比较字符串的错误。

    (另见:How do I compare strings in Java?

    【讨论】:

      【解决方案2】:

      使用 assertEquals() 而不是 assertSame(),因为您不能将 assertSame() 与字符串一起使用。 .参考这个JUnit asserting two Strings whether they're equal or not

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-12-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多