• Post Reply Bookmark Topic Watch Topic
  • New Topic

Strange JUnit Testresult  RSS feed

 
Dirk Wierdemann
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Scott Selikoff
author
Bartender
Posts: 4093
21
Eclipse IDE Flex Google Web Toolkit
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah Ernest, you beat my response by like 30 seconds, oh well, nevermind you covered everything as usual .
 
Dirk Wierdemann
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!