英文:
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 = "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?
答案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
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论