When it comes to test your code, is it a good idea to use its own main for testing?
The other option would be to use an external file with this tests.
An example:
Class Foo{
Foo(){/*...*/}
public boolean met1(){/*...*/}
//other metodes...
//Tests
public static void main(String args[]){
Foo f = new Foo();
//Test the correctness of data...
boolean r = f.met1();
//check the result...
}
}
What are the pros and cons of this technic.
Maybe there are better approaches for testing code that i did not mention.
If it is a test that can be truly automated, you should use an external file, as for JUnit etc. Lots of support for running all the tests, continuous integration, reporting back with nice green and red lines, etc…
If it is a GUI element that you want to be able to bring up and see what it looks like, and perhaps manually click on places to see what happens, it’s o.k. to use main(). Comment as such.
Likewise, if it is something complex that a programmer might want to test and watch what’s going on in the debugger, vary the command line args, etc. it’s o.k. to use a main(). Comment as such.