File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Using the Main method as a testing tool

 
Stephen Adams
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
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
Posts: 8791
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 http://www.JUnit.org. The Getting Started link has another link to Test Infected which is a good (maybe overly detailed) intro to unit testing.
 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

HTH
Steve
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.
Cya
 
Gabriel White
Ranch Hand
Posts: 233
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hmm, that seems pretty awesome Stan, I'll have to check that out.
Thanks bro
Steve
 
Stephen Adams
Greenhorn
Posts: 22
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
cheers guy, this helps clear up some questions. I give it and try and see how it goes, thanks for the help.
Stephen
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic