Java J2EE Portal
Enterprise Java Station
J2EE curve
Java News / Articles
Java News / Articles
Oracle OpenWorld
Fusion Middleware, Virtualization and more from Oracle OpenWorld 2007
Kill The Java Language - A Message For Scripting Language Leads
Password Based Authentication Using Message Digests
Processing...
Buy Java, Deals On Software Technology Store
Click here for great deals on computers, laptops, software and books
Let's Use JUnit PDF Print
Written by Varun Chopra   
May 26, 2005 at 04:41 PM

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:

  1. If one method call fails, next method calls won?t be executed. You will have to work-around this.
  2. 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.
  3. If you want the test results to be displayed in a GUI, you will have to write code for that GUI.
  4. If you want to log the results of tests in HTML format or text format, you will have to write additional code.
  5. Your classes will be cluttered with test code in main method.
  6. 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.




Add This Feed Button

Enter your Email


Java Expert Interviews
GraemeRocher-Grails
Grails is a breath of fresh air for Java developers
MarkSchiefelbein-BackbaseAjaxFramework
All Ajax development can happen serverside using the Backbase framework and JSF
Open-Source keeps me 'coding fit'
Processing...
Go to top of page  Home |
SiteMap

Copyright 2004 to 2008 Rightrix Solutions. All rights reserved. All product names are trademarks of their respective companies. Java and all Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. Rightrix Solutions and IndicThreads.com are independent of Sun Microsystems, Inc.

Views expressed at IndicThreads.com reflect the views of the authors alone, and do not necessarily reflect those of IndicThreads.com. IndicThreads.com and it's authors are not responsible for reader comments and opinions.

Enterprise Java J2EE JEE Portal >> IndicThreads.com