• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

why JUnit?

 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have never used junit or any other unit testing tool, can anybody explain what are the benefit of using them.
I use JDeveloper, and i can trace any bug by running the application in debug mode. so do why we need tools like JUnit?
do they provide some extra benefits?
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What you're doing now (debugging) is manual work while JUnit tests are automatic. While you step through your code manually, you could've run your whole unit test suite tens or hundreds of times...
Also, because the tests are automated and fast, you are able to test the whole codebase for regression very easily -- something you probably wouldn't do too often if you only have a debugger at your disposal.
 
Andy Hunt
Author
Ranch Hand
Posts: 55
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'd just like to echo the previous poster's comments.
Debugging and unit testing are different activities. When debugging, you're trying to figure out what went wrong (or at least, what the code is doing).
With unit testing, you start off by setting an expectation that the code should do XYZ. When it doesn't, then you can track down and fix the problem. But now, the test code will continue to check that the code does XYZ properly forever, without any further effort on your part. It's like have a Genie come along behind you, who keeps running the debugger to make sure that everything still works.
The whole point of unit testing is to validate your assumptions. To prove that the code is doing what you think it should, all the time, under a variety of input conditions. Without it, you are never quite sure where the bugs are.
[ February 17, 2004: Message edited by: Andy Hunt ]
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prakash Dwivedi:
I use JDeveloper, and i can trace any bug by running the application in debug mode. so do why we need tools like JUnit?
do they provide some extra benefits?

What does a typical debug session look like? To me it starts with guessing where the bug might be. Set a break point, debug - well, seems I guessed wrongly.
So I put the break point some levels up in the call tree and observe the parameters to, and return values from methods. Probably I will find some method which returns a value it shouldn't return or something. So I take a look at that method etc.
Finally, I know that the bug has to be in a specific small method. It's puzzling, because the method looks "obviously correct". So I step through it - Doh! That was so obvious, I must have been blind some weeks ago, when I wrote the method...
----
Here is what happens when I use JUnit instead of the debugger:
Write the code and the test (or the other way round), run the test - it fails. Oops. Ah, yes, that should have been j instead of i - run test again - passes.
In my opinion, the latter is much more fun than the former.
 
Prakash Dwivedi
Ranch Hand
Posts: 452
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the reply,
what i have understood is that by using tools like JUnit, i can test my method for different parameters, in far less amount of time than doing it through debugger.
But in JUnit i have to write the test scipts for each method, how easy is this part?
as I have download JUnit and i'll be trying it for tghe first time.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prakash Dwivedi:
But in JUnit i have to write the test scipts for each method, how easy is this part?

Quite easy, given you have a testing-friendly design.
 
Lasse Koskela
author
Sheriff
Posts: 11962
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Prakash Dwivedi:
But in JUnit i have to write the test scipts for each method, how easy is this part?
Ah, but you don't have to. You can. The idea is not to test absolutely everything (for example, people generally don't write dedicated test method for testing getters and setters).
 
Serge Plourde
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Unit testing is not "just for debugging". It is part of a whole new approach for software development.
You have an idea. You test it *first*. Then you refine your idea, which is tested again, and so on. In doing so, you keep your tests.
When, later, you need to add to your code, or make any corrections, you run the existing tests and you will see right away if you broke something.
I am just starting to work with JUnit, and I am starting to like it. It gives you more confidence in what you release.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic