title: How to set the exit status in a Groovy Script
body: <p>We have a Groovy Script that exits with a <code>status</code> of <code>0</code> when everything worked and a <code>non-0 status</code> for different types of failure conditions. For example if the script took a user and an email address as arguments it would exit with a <code>status</code> of <code>1</code> for an invalid user, and a <code>status</code> of <code>2</code> for an invalid email address format. We use <code>System.exit(statusCode)</code> for this. This works fine, but makes the the script difficult to write test cases for.</p>
  
  <p>In a test we create our <code>GroovyShell</code>, create our <code>Binding</code> and call <code>shell.run(script,args)</code>. For tests that assert failure conditions, the <code>System.exit()</code> causes the JVM (and the test) to exit.</p>
  
  <p>Are there alternatives to using <code>System.exit()</code> to exit a Groovy Script? I experimented with throwing uncaught exceptions, but that clutters the output and always makes the status code 1. </p>
  
  <p>In our test cases I have also experimented with using <code>System.metaClass.static.invokeMethod</code> to change the behavior of <code>System.exit()</code> to not exit the JVM, but that seems like an ugly hack.</p>