File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Testing void method in Junit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Testing void method in Junit" Watch "Testing void method in Junit" New topic

Testing void method in Junit

Tom Mordon
Ranch Hand

Joined: Apr 26, 2012
Posts: 30

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

Joined: Apr 06, 2010
Posts: 4543

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

Joined: Apr 26, 2012
Posts: 30
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.
I agree. Here's the link:
subject: Testing void method in Junit
It's not a secret anymore!