| Author |
Strange JUnit Testresult
|
Dirk Wierdemann
Greenhorn
Joined: Nov 04, 2005
Posts: 2
|
|
Hello there, I get a rather strange testresult. Would be very pleased if somebody could explain: Testcode: public class PreisTest extends TestCase { Preis preis; protected void setUp() throws Exception { preis = new Preis( "01/01/01", 10.5 ); super.setUp(); } public void testEqualsPreis() { Preis test = new Preis( "01/01/01", 10.5 ); assertTrue( test.equals( preis )); // green bar assertEquals( test, preis ); // red bar } } CUT-Code: public class Preis { private String publishMonth; private double preis; public Preis(String publishMonth, Double preis) { this.preis = preis; this.publishMonth = publishMonth; } //getter, setter and toString methods here //all successfully tested... public boolean equals( Preis actual ) { boolean result = false; if( preis == actual.getPreis() && publishMonth.equals( actual.getPublishMonth())) { result = true; } return ( result ); } } Thanks in advance Aph.&more [ November 04, 2005: Message edited by: Dirk Wierdemann ]
|
 |
Ernest Friedman-Hill
author and iconoclast
Marshal
Joined: Jul 08, 2003
Posts: 24039
|
|
Hi, Welcome to JavaRanch! When you override a method, the signature must match exactly, and in particular, the argument types must be exactly the same. Otherwise, what you're doing is defining a new method altogether. JUnit uses the standard equals(Object) inherited from java.lang.Object, but you haven't actually overridden that method; instead you've overloaded it, or defined a new method with the same name. The inherited version is still available, though, and that's what JUnit is using. Whenever you override equals(), it should look like Finally, note that whenever you override equals(), you should override hashCode(), too -- or your class won't work properly in many of the standard container classes, or in other situations. See the Javadoc for equals() and hashCode() in java.lang.Object for details.
|
[Jess in Action][AskingGoodQuestions]
|
 |
Scott Selikoff
Saloon Keeper
Joined: Oct 23, 2005
Posts: 3652
|
|
Ah Ernest, you beat my response by like 30 seconds, oh well, nevermind you covered everything as usual .
|
My Blog: Down Home Country Coding with Scott Selikoff
|
 |
Dirk Wierdemann
Greenhorn
Joined: Nov 04, 2005
Posts: 2
|
|
Seems like I misinterpreted the term ( Object o) in the documentation. I thougt, that any object would do, not that I should use class Object... Thank you very much... ][))irk
|
 |
 |
|
|
subject: Strange JUnit Testresult
|
|
|