wood burning stoves 2.0*
The moose likes Testing and the fly likes why JUnit? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » Testing
Bookmark "why JUnit?" Watch "why JUnit?" New topic
Author

why JUnit?

Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
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?


Prakash Dwivedi (SCJP2, SCWCD, SCBCD)
"Failure is not when you fall down, Its only when you don't get up again"
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
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.


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Andy Hunt
Author
Ranch Hand

Joined: May 01, 2001
Posts: 54
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 ]

/\ndy Hunt<br /> <a href="http://www.PragmaticProgrammer.com" target="_blank" rel="nofollow">www.PragmaticProgrammer.com</a>
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
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.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
Prakash Dwivedi
Ranch Hand

Joined: Sep 28, 2002
Posts: 452
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

Joined: Jul 11, 2001
Posts: 14112
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

Joined: Jan 23, 2002
Posts: 11962
    
    5
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

Joined: Jun 23, 2000
Posts: 140
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.
 
wood burning stoves
 
subject: why JUnit?
 
Similar Threads
Junit advantages
Issues installing junit on linux
Novie JUNIT user need helps - NoClassDefFoundError + bash + windows
Passed Urlybird 1.3.3 with 377/400!
Eclipse + JUNIT_HOME