File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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

Win a copy of Make it so: Java DB Connections & Transactions this week in the JDBC forum!
JavaRanch » Java Forums » Engineering » Testing
Bookmark "My First Test with JUnit" Watch "My First Test with JUnit" New topic

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
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.



Dave Thomas <br />Author of "<a href="" 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

Joined: Jan 23, 2002
Posts: 11962
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:
subject: My First Test with JUnit
It's not a secret anymore!