JUnit说测试失败了。看差异说内容是相同的

时间:2013-07-14 18:26:44

标签: junit intellij-idea assert

JUnit说测试失败了。但是当我按下看差异链接时,它显示内容是相同的。那么,如果两个对象相同,为什么测试失败呢?

我正在进行的测试。

 Assert.assertSame(card,vCardService.DecodeVCard(text));

当我按下“看到差异”链接

时会显示

enter image description here

错误。

 Expected :Card [CardId=1, UserId=0, FirstName=Joel, LastName=Dean,     WorkNumber=18769023684, CellNumber=18765757810, Email=jdeanjj1000@gmail.com, Title=Mr., UserProfileImageUrl=null, CompanyName=Xormis, CompanyAddress=null, CompanyLatitude=0.0, CompanyLongitude=0.0, LogoUrl=null, QrCodeUrl=null, QrVCardUrl=null, VCardUrl=null, FacebookUrl=null, Website=null, ShareCount=0]
Actual   :Card [CardId=1, UserId=0, FirstName=Joel, LastName=Dean, WorkNumber=18769023684, CellNumber=18765757810, Email=jdeanjj1000@gmail.com, Title=Mr., UserProfileImageUrl=null, CompanyName=Xormis, CompanyAddress=null, CompanyLatitude=0.0, CompanyLongitude=0.0, LogoUrl=null, QrCodeUrl=null, QrVCardUrl=null, VCardUrl=null, FacebookUrl=null, Website=null, ShareCount=0]
 <Click to see difference>
    at org.junit.Assert.fail(Assert.java:88)
    at org.junit.Assert.failNotSame(Assert.java:737)
    at org.junit.Assert.assertSame(Assert.java:680)
    at org.junit.Assert.assertSame(Assert.java:691)
    at com.grikly.app.test.VCardServiceTest.testDecodeVCard(VCardServiceTest.java:68)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:47)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:44)
    at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17)
    at org.junit.internal.runners.statements.RunBefores.evaluate(RunBefores.java:26)
    at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:271)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:70)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:50)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:238)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:63)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:236)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:53)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:229)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:309)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:160)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:76)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:195)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:63)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:120)

3 个答案:

答案 0 :(得分:4)

你想要assertEquals; assertSame,如果它们是同一个对象。

答案 1 :(得分:0)

我有同样的问题,但在比较2个String对象(!!!)时。 事实证明,如果你使用

,IntelliJ就不那么聪明了

断言(X,是(Y));

它不会显示'\ n'和'\ n'之间的区别 - 即使它存在。 我可以建议使用assertEquals()代替,然后在问题所在的位置上会非常明显。

答案 2 :(得分:0)

IntelliJ会告诉您“内容相同”,即使这两个字符串因行结尾的样式而不同。例如,如果实际字符串具有Unix样式结尾且预期字符串具有Windows样式结尾,则IntelliJ将显示“内容相同”,但断言将失败。