Login | Register   
LinkedIn
Google+
Twitter
RSS Feed
Download our iPhone app
TODAY'S HEADLINES  |   ARTICLE ARCHIVE  |   FORUMS  |   TIP BANK
Browse DevX
Sign up for e-mail newsletters from DevX


Tip of the Day
Language: Java
Expertise: Beginner
Sep 23, 1997

Include Test Code in Your Classes

It is important for developers to test code that they write to insure that they are meeting the specifications for their application. Often test code is built in a separate program which loads and tests a particular class. Instead, you can include test code directly inside your class and place it in a "main" method.

When you run a Java program from the command line:

 java ApplicationStartingClass
you supply the name of the class that contains the start up code. Java execute a specific method in that class, the "main" method:
 public class ApplicationStartingClass
{
   public static void main( String[] args )
   {
      . . .
   }
}
You can use this feature of Java to embed test code inside each of the classes that you write. If you are working developing the class Person, you can include a main method in the class, even though it will never be called during normal use.
 public class Person
{
   . . .
   public static void main( String[] args )
   {
      // include test code which exercises the class Person
   }
}
When you need to test your class, you simply run the class from the command line:
 java Person
Randy Kahle
 
Comment and Contribute

 

 

 

 

 


(Maximum characters: 1200). You have 1200 characters left.

 

 

Sitemap
Thanks for your registration, follow us on our social networks to keep up-to-date