File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Strange JUnit Testresult Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Mongo DB Applied Patterns this week in the MongoDB forum
or a resume review from Five Year Itch in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Strange JUnit Testresult" Watch "Strange JUnit Testresult" New topic
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
    
  13

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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Strange JUnit Testresult
 
Similar Threads
help building a class
HFJ SimpleDotCom problems
Polynomials and Hashing
Strings
can't run this program