This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I am new to the eclipse IDE. How to identify whether the failure trace which we get upon running a test case is belongs to the current test case or any previous one's. Since I am not getting any failure trace eventhough an expected is other than result.
Eclipse's JUnit GUI associates an assertion failure with the test method that faile. It also allows you to double click on the failuse to be taken to the line the assertion that failed was on, so it should be easy enough to find out.
Since I am not getting any failure trace eventhough an expected is other than result
I'd check the logic of your test case. Is the assertion as you expect it to be?
I am getting AssertionFailedError in testGetEmployee() and testRemoveEmployee() eventhough expected and the result are same.
Looking just at testGetEmployee():
I would expect an AssertionError to be thrown at line three, since I don't see anywhere that you add an Employee object to your collection with the key "dd".
Also, looking at your Employee class you override equals() but you don't override hashCode(). Yo0u will need to override this too. [ January 12, 2007: Message edited by: Paul Sturrock ]
Joined: Dec 14, 2006
Please find the failure trace. As per the trace the expected and the result are same but why an error is thrown? Could You please the post code for the hashCode() for the Employee object here in the reply.
As per the trace the expected and the result are same but why an error is thrown
The results are not the same. An object is equal if its equals method returns true and it has the same hashCode value as the object you are comparing it to. Since you have not overridden hashCode() its unlikely they will have the same hashCode value.
Could You please the post code for the hashCode() for the Employee object here in the reply
How you implement hashCode() is up to you. If you look at how the String class does it:
It is basically derived from the three properied of a String - value, offset and count. You'll need something similar for your Employee object. Here is an article which covers some of the factors you might consider. [ January 12, 2007: Message edited by: Paul Sturrock ]
Paul, I think you are following the wrong trail, the hashcode method isn't the problem - it isn't used by the test.
Vijayk, the problem is that by implementing
public boolean equals(Employee x)
you haven't overridden
public boolean equals(Object)
but you have *overloaded* it. Your custom equals method simply isn't used at all. [ January 12, 2007: Message edited by: Ilja Preuss ]
The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus