Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

My First Test with JUnit

 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Dave Thomas
Author
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic