Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Testing void method in Junit

 
Tom Mordon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


I just started using Junit and Netbeans IDE after using the command line compiler last year. I'm trying to get my code to pass the junit tests. I would rather use if() and .equals rather than assertEquals(). The above code is just one of many examples I have but if I can get the hang of this one I think I can do knock the rest out. It's telling me "double cannot be dereferenced". Been stuck on this the few days so finally throwing in the towel and reaching out for help.

 
Matthew Brown
Bartender
Posts: 4567
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm assuming getExtendedPrice() is returning a double, right? double is a primitive value, so it doesn't have an equals() method. You can use:

However, I'd really suggest getting used to using the assert methods. Why don't you like them? They give you much richer feedback in general - e.g. the framework can tell you "the expected value was 100.0, but the actual value was 99.9" (or something like that). Yes, you could do that yourself, but it's more work.

I also think assertion methods make the test more obvious and readable, which is really important.
 
Tom Mordon
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes the getExtendedPrice() returns a double. Thanks much works just fine now, makes sense also. I just reread the testing rules and he wants us to use if() rather than what pops up automatically, in class the teacher used .equals() but I went over the class video again and assertEquals() is ok as long as we use in in and if() with the fail() within the if.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic