File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Using the Main method as a testing tool Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Using the Main method as a testing tool" Watch "Using the Main method as a testing tool" New topic

Using the Main method as a testing tool

Stephen Adams

Joined: Mar 18, 2002
Posts: 22
I was reading my copy of Thinking In Java the other day and in it Bruce Eckel said that you can write a main method in any class you write, even if you don't start a application for this main method. He says that you can use the main method as a testing tool for your class, as a newbie to Java I was wondering if and how anyone else uses this method in this way?
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
This was my favorite technique until I found JUnit. I wrote a lot of things where I ran main and visually inspect a lot of displays. One advantage to this is the tests are always available any time the class is available.
JUnit moves the tests into seperate classes which has pros & cons. The JUnit framework encourages you to check test results programatically instead of visually. And you can aggregate a bunch of tests into a regression suite that is easy to run over and over.
If you haven't met JUnit yet, see The Getting Started link has another link to Test Infected which is a good (maybe overly detailed) intro to unit testing.

A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
I think what they are saying is just that you can use the main method as a way to test your public methods or other classes as a harness. Since the main method is a static argument it can be seen by the entire program and thus is the engine for the entire program. For instance, say you wrote a program like this, with just a simple message being printed out. Normally you would just put this in the main method but you can test it by making another method and calling it with the main method by instantiating an object to call that public method.
here is what I mean:

by the way, I didn't get a chance to compile this so I apologize if there are any errors with your IDE or whatever you are using.
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Hmm, that seems pretty awesome Stan, I'll have to check that out.
Thanks bro
Stephen Adams

Joined: Mar 18, 2002
Posts: 22
cheers guy, this helps clear up some questions. I give it and try and see how it goes, thanks for the help.
Ilja Preuss

Joined: Jul 11, 2001
Posts: 14112
As Stan, I am mostly using JUnit today.
There are some aspects which are still more easy to check visually, though - for example the layout of a Swing dialog. For this, JUnit simply isn't the right tool.
The main method has some disadvantages nevertheless, though:
- a class can only have one, even if you wanted to try different aspects of it,
- the test code isn't as well organized as in JUnit TestSuites, and
- the test code isn't separated from the production code
A coworker of mine is currently working on a tool he calls JDemo, which is to those manual tests what JUnit is to automated tests. I expect him to publish it in the next weeks...

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
Jim Yingst

Joined: Jan 30, 2000
Posts: 18671
I still do use the main method for testing sometimes, especially for a "quick & dirty" class - most often, to demonstrate something here at JavaRanch. It's nice to show a couple simple tests without expecting the reader to have JUnit set up already. However for any serious project, JUnit is the way to go. Except for some exceptions as Ilja noted.

"I'm not back." - Bill Harding, Twister
I agree. Here's the link:
subject: Using the Main method as a testing tool
It's not a secret anymore!