Java Habits for main method
java
Solution
JUnit lets you have tests, just like your mains, but also:
- a main is typically only one method, that can get real big ; if extracting small methods used only for test, there is a risk to use that methods in regular code
- doesn't clutter the class itself with the testing methods, they are in a different class
- allows inheritance in the test classes (main, as a static method, is impossible to inherit or reuse) ; typically, the setup before the actual test maybe pretty long, and reusing it naturally is great
- a main has no result (success or failure), only an output ; you need to check manually the output to determine the result, and possibly understand it
- allow execution of several tests (class, package, project, all) at once, which is required for regression testing (or you will spend your afternoon executing them one by one)
- JUnit provide many additional features out of the box, like marking some tests as ignored, checking that a test is not too long, provide several launching UIs etc.
- you can reuse some tests on each implementation or subclass (ex: checking for Liskov substitution), which allows you to test a lot without maintaining a lot of test code.
Problem
I write code primarily for personal use, but I'm considering releasing an application (scientific simulation/visualization) that I originally developed for personal use. One of my habits is to use a main method in classes for testing the operation of the class in isolation. I figure that's probably bad in someway (as are no doubt various other habits originating from self-teaching and the scientific development environment). However, it's never been a problem for self-use stuff that I've noticed. Would you all be so kind as to confirm (or deny) that the proliferation of mains is a problem for an application released to the scientific community (the source would also be open), and if so, why? EDIT: To play devil's advocate (okay, my advocate) relative to some of the offered answers: part of the "application use" is expected to be source modification by non-developers (the typical scientist) on a smallish scale. I know that on the receiving end, that having the tests for a class built directly into that class would be pretty straightforward for me to recognize and modify accordingly (especially if that were consistently the case for the classes). Would using something like JUnit provide similar utility, keeping in mind the audience? ACCEPT DECISION: I think KLE's answer is the best balance of thorough and succinct, so I picked it, but I think the discussion comments in Bill's are also very helpful. I also don't understand why Johannes's answer was voted down - the "how does this piece work" perspective is very important to the scientific community coders - and while the other answers point out various reasons why separated unit tests are probably more useful than my current habit, they don't really address that use, so his answer is far from "unhelpful". Thanks to all current (and future) responders, and here's to wishing there was a way to combine multiple responses as the correct answer!