This week's book giveaway is in the Mac OS forum.
We're giving away four copies of a choice of "Take Control of Upgrading to Yosemite" or "Take Control of Automating Your Mac" and have Joe Kissell on-line!
See this thread for details.
The moose likes Testing and the fly likes My First Test with JUnit Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Engineering » Testing
Bookmark "My First Test with JUnit" Watch "My First Test with JUnit" New topic
Author

My First Test with JUnit

Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Following is the script of first unit test i executed.
what i am unable to understand is, what JUnit is doing in this testing?
I am writing whole java program, and checking myslef the result in the code.
assertXXX are Only methods which JUnit is providing. I don't think it will be very difficult for any developer to write code for these methods.
Also if i have to execute this script for 100 different cases, what changes i have to make in this java program?


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Dave Thomas
Author
Ranch Hand

Joined: May 01, 2001
Posts: 45
I think partly your concern here is that you've written a boatload of code that doesn't really seem to you to have much value, and you're questioning whether it's worthwhile.
As a way of experimenting with testing, I'm sure it is: you've learned how to run a JUnit test (although it doesn't have to be as complicated as this example) and you've learned how to separate the test from the code.
But at the same time, your questions have a lot of merit, because in this case there's not much to be tested. I personally never write unit tests for getters and setter for just this reason.
But say you were writing some code to calculate an order total:

Here, even though you wrote the original code, it would still be a good idea for you to write tests for it too. You know what you think the code should do, and you may even have a spec. So write the test, feeding in different combinations of line items, and verify that your code handles it all correctly. Does it deal with quantoity discounts, multiples of the same line item, or even orders with no line items? All these are valid things to test, and thinking about them while you're coding might even help you design a better method.
And this is the key to the benefit of unit testing. It makes you think more about the stuff you write.

Cheers

Dave


Dave Thomas <br />Author of "<a href="http://www.amazon.com/exec/obidos/ASIN/020161622X/ref=ase_electricporkchop/002-7467239-7061602" target="_blank" rel="nofollow">The Pragmatic Programmer: From Journeyman to Master</a>
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
Thanks for the reply Dave,
I was testing setters / getters as this was my first test, and i was more interested in learning how to do unit testing through JUnit than testing my code.
But my doubt about role of JUnit in this test still remain as it is? I am not disputing the importance of unit testing here, but if i have to write the whole code (big or small) what JUnit is doing in between?
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Prakash Dwivedi:
I am not disputing the importance of unit testing here, but if i have to write the whole code (big or small) what JUnit is doing in between?
JUnit provides a framework and the necessary interfaces for running the tests and reporting the results in a Swing GUI, plain text or in XML (which can be transformed into HTML). This would be a lot of work to do by yourself!
Also, you don't have to write a suite() method for each TestCase class -- only when the default, reflection based test suite collector doesn't fit your needs (if you don't provide a suite() method, the JUnit TestRunner will pick up the individual test methods via reflection).


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: My First Test with JUnit