| |
|
Let's Use JUnit |
|
|
|
Written by Varun Chopra
|
|
May 26, 2005 at 04:41 PM |
|
Page 1 of 5 Although JUnit is being projected as a standard in
testing java code but still there are many Java developers who have not
used it. One reason is lack of practical tutorials
with examples. In this tutorial, I have attempted to present Junit
usage in practical way with actual examples. There is lot more that you
can do with JUnit (beyond this tutorial) but it will kick start you
well, I believe.
Okay, let?s proceed.
Let's Use JUnit
First obvious question, why to use JUnit when we
can write a main() method to test individual methods of a class?
Answer to this is ?better management?. If you
write calls to individual methods in main() method, you will have to
write a lot of code to reach where you want to reach. Here?s a list of
points:
- If one method call fails, next method calls
won?t be executed. You will have to work-around this.
- Generally, you will execute main() of a class
only when you are creating the class (that?s a bad habit though). If
you want to execute main() of every class, every time code is changed,
you will have to provide support for this yourself.
- If you want the test results to be displayed in
a GUI, you will have to write code for that GUI.
- If you want to log the results of tests in HTML
format or text format, you will have to write additional code.
- Your classes will be cluttered with test code
in main method.
- If you start working on a project created by
some other team in your organization, you may see an entirely different
approach for testing. That will increase your learning time and things
won?t be standard.
Above are some of the problems,
which will be taken care of automatically if you use Junit. Junit
provides a standard framework for writing your tests. It separates test
code from project code, hence keeps everything clean.
Let?s now move forward to learning its usage.
<< Start < Previous 1 2 3 4 5 Next > End >> |
|
|
|