JUnit断言错误,字符串相同。

huangapple go评论72阅读模式
英文:

JUnit Assertion error with identical strings

问题

以下是您要翻译的内容:

"I'm trying to make a test-case to check for the Dropwizard Metric Meter name:
Here's the code:

@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));
}

This is the error I'm getting:

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

What am I missing here?"

英文:

I'm trying to make a test-case to check for the Dropwizard Metric Meter name:
Here's the code:

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

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

This is the error I'm getting:

java.lang.AssertionError: expected same:&lt;com.company.team.tenant.db.table&gt; was not:&lt;com.company.team.tenant.db.table&gt;
Expected :com.company.team.tenant.db.table
Actual   :com.company.team.tenant.db.table

What am I missing here?

答案1

得分: 5

以下是翻译好的部分:

What am I missing here?

这里我漏掉了什么?

The strings that you are testing are equal but not the same object.

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

You are using assertSame where you should be using assertEquals.

你正在使用 assertSame,但应该使用 assertEquals

This is analogous to the mistake of using == to compare strings.

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

(See also: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)
(参见:https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)

英文:

> What am I missing here?

The strings that you are testing are equal but not the same object.

You are using assertSame where you should be using assertEquals.

This is analogous to the mistake of using == to compare strings.

(See also: https://stackoverflow.com/questions/513832/how-do-i-compare-strings-in-java)

答案2

得分: 0

使用assertEquals()而不是assertSame(),因为你不能在字符串上使用assertSame()。
请参考JUnit断言两个字符串是否相等

英文:

Use assertEquals() instead of assertSame() because you can't use assertSame() with Strings.
.Refer this JUnit asserting two Strings whether they're equal or not

huangapple
  • 本文由 发表于 2020年8月8日 15:34:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/63312898.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定