title: How do you mock an object instantiated inside another object in Groovy?
body: <p>Here's my example code</p>
  
  <pre><code>class CommandLine {
  
      def ls() {
          def cmd = "ls".execute()
          if(cmd.waitFor() != 0) {
              throw new Execution()
          }
          return cmd.text
      }
  }
  </code></pre>
  
  <p>The <code>cmd</code> variable holds an object of type java.lang.Process.  How would I mock out the <code>waitFor()</code> method in order to test the thrown exception?  If I can't, is there some way this could be rewritten to facilitate automated testing?</p>
  
  <p>In general, how do you mock an object instantiated inside another class, or how do you structure your code to allow for testing?</p>