• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Using the Main method as a testing tool

 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
(instanceof Sidekick)
Posts: 8791
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 233
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic