JUnit AssertSame 在 groovy 中的 object.toString 上失败
我正在尝试在 groovy 中测试重写的 toString() (我知道这是微不足道的,但这就是您在阅读 kent beck 的 TDD 书后得到的结果)。 我对预期字符串和实际字符串断言相同
这是代码块:
@Test void testToString(){ def study = new Study(identifier:"default-study", OID:"S_DEFAULTS1", name:"Default Study") def expected = "org.foo.oc.model.bar(OID:S_DEFAULTS1, name:Default Study, identifier:default-study)" assertSame "Should be equal", expected, study.toString() }
这是失败测试的堆栈跟踪:
junit.framework.AssertionFailedError: Should be equal expected same:org.foo.oc.model.bar(OID:S_DEFAULTS1, name:Default Study, identifier:default-study) was not:org.foo.oc.model.bar(OID:S_DEFAULTS1, name:Default Study, identifier:default-study) at junit.framework.Assert.fail(Assert.java:47) at junit.framework.Assert.failNotSame(Assert.java:273) at junit.framework.Assert.assertSame(Assert.java:236)
只是补充一点,assertEquals 对于相同的参数可以很好地工作。 我知道这没什么大不了的,但我想了解为什么会失败。
谢谢
I am trying to test the overridden toString() in groovy (I know it is trivial but that is what you get after reading kent beck's TDD book).
I assertSame on the expected string and the actual
Here is the code block:
@Test void testToString(){ def study = new Study(identifier:"default-study", OID:"S_DEFAULTS1", name:"Default Study") def expected = "org.foo.oc.model.bar(OID:S_DEFAULTS1, name:Default Study, identifier:default-study)" assertSame "Should be equal", expected, study.toString() }
Here is the stack trace for the failed test:
junit.framework.AssertionFailedError: Should be equal expected same:org.foo.oc.model.bar(OID:S_DEFAULTS1, name:Default Study, identifier:default-study) was not:org.foo.oc.model.bar(OID:S_DEFAULTS1, name:Default Study, identifier:default-study) at junit.framework.Assert.fail(Assert.java:47) at junit.framework.Assert.failNotSame(Assert.java:273) at junit.framework.Assert.assertSame(Assert.java:236)
Just to add that assertEquals works well with the same parameters.
I know it is no biggie but I want to understand why it fails.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
为什么不使用使用
.equals()
的assertEquals
?assertSame
比较对象引用(==
运算符)。尽管字符串相同,但它们是两个不同的对象,因此断言失败。更新:这是 Java 中一个非常常见的错误:
String.equals()
和==
运算符的工作方式不同。这已经讨论过多次:我知道您正在使用 Groovy,它不会遇到这个问题,但 JUnit 是用 Java 编写的,并且按照上述规则运行。
更新:实际上,您的字符串不同:Why aren't you using
assertEquals
which uses.equals()
?assertSame
compares object references (==
operator). Even though the strings are the same, they are two different objects, hence the assertion is failing.UPDATE: This is a very common mistake in Java:
String.equals()
and==
operator work differently. This has been discussed several times:I know you are using Groovy which does not suffer this problem, but JUnit is written in Java and behaves according to the rules above.
UPDATE: actually, your string are different:您的原始文件在“default Study”中使用小写 d,但您期望的字符串却没有。
编辑:比较字符串时,您应该始终使用 equals() 而不是比较引用。通过 equals() 测试的两个字符串可能是也可能不是同一个对象。
顺便说一句,在 Groovy 中 == 与 equals() 相同。
Your original uses lowercase d in "default Study" but your expected string does not.
EDIT: when comparing strings you should always use equals() rather than comparing references. Two strings that pass the equals() test may or may not also be the same object.
BTW, in Groovy == is the same as equals().