title: How do I force a method in Groovy to throw an exception
body: <p>I wanted to write a test for a method in Groovy that throws an IOException. The only way for me to simulate this in the test is to force the method to throw this exception 
  This is what the original code looks like:</p>
  
  <pre><code>    public void cleanUpBermudaFiles(RequestMessage requestMessage)
  
      {
      final File sourceDirectory = new  File(preferenceService.getPreference("bermuda.landingstrip") + File.separator + requestMessage.getWorkflowId().getValue());
  
      if(sourceDirectory!=null &amp;&amp; sourceDirectory.exists())
      {
           deleteDirectory(sourceDirectory);
      }
      else
      {
          LOG.error("Directory must exist in order to delete");
      }
  
  }
  
  private void deleteDirectory(File directoryToDelete)
  {
      try {
          FileUtils.deleteDirectory(directoryToDelete);
      } catch (Exception e) {
          LOG.error("Failed to delete Bermuda files directory located at:" + directoryToDelete.getPath() + "with an exception" + e.getMessage());
      }
  }
  </code></pre>
  
  <p>MY TEST: (I'm looking for a way to make deleteDirectory throw IOException)</p>
  
  <pre><code>     public void testCleanUpBermudaFailure()
      {
      workflowId = new WorkflowId("123456")
      workflowDirectory = new File(srcDirectory, workflowId.value)
      workflowDirectory.mkdir()
      File.createTempFile('foo','.lst', workflowDirectory)
      def exception = {throw new IOException()}
  
      expect(mockRequestMessage.getWorkflowId()).andReturn(workflowId)
      expect(mockPreferenceService.getPreference("bermuda.landingstrip")).andReturn(srcDirectory.path)
      replay(mockPreferenceService, mockRequestMessage)
      fileCleanUpService.preferenceService = mockPreferenceService
      fileCleanUpService.metaClass.deleteDirectory = exception
      fileCleanUpService.cleanUpBermudaFiles(mockRequestMessage)
      verify(mockPreferenceService, mockRequestMessage)
      assert srcDirectory.listFiles().length == 0, 'CleanUp failed'
  
  }
  </code></pre>