This indeed seems a bit hacky to me. You definitely shouldn't be using an exception to tell the user that they're using the application wrongly. Exceptions are for when things within your application don't work properly. That means that when the program requires main method arguments, you check them explicitly and then print a usage string to standard output.
I also don't really understand why you're explicitly calling System.exit(0). When the main method finishes running, the application will automatically finish with an exit code.
Anyway, to unit test standard output, you can replace standard output and error with print writers that write to an byte array stream. You can then check the contents of the byte array stream to see if the correct output was written.
If you do need to test System.exit(), there's a rule in this JUnit add on for it. It has a class called ExpectedSystemExit which uses a security manager to change behavior. I recommend re-writing your code to avoid the System.exit though.
phil stephens wrote:I have a few calls to System.exit preceded by a message to screen with and explanation as to why...
Well, like Stephan (and probably Jeanne too), that just strikes me as wrong.
There is almost never a good reason for calling Sysem.exit() explicitly; and if you do, you generally shouldn't be calling System.exit(0) - the reason being that 0 is what is returned when the program ends normally.
Secondly: any uncaught Exception will cause the program to terminate anyway, so calling Sysem.exit()inside an Exception seems doubly redundant.
I'm having ExitCodeExceptionMapper in main class. that will exit app with different exitcode according to the exception caught(exception from all files). stuck at writing test for different cases and it's exit code(1/2/3).?