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"
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.
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>
Joined: Sep 28, 2002
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?
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).