title: Is it possible to mock an enum with Spock?
body: <p>I have a switch statement to handle an java enum foo, and am using spock to write some groovy unit tests. I have already added a test which verifies that every type of foo is currently handled without throwing an exception. Now I want to test that an unrecognised type of foo will cause an exception to be thrown. </p>
  
  <p>To do this I will have to mock an enum, and have already seen the solution outlined here:
  <a href="https://stackoverflow.com/questions/5323505/mocking-java-enum-to-add-a-value-to-test-fail-case">Mocking Java enum to add a value to test fail case</a></p>
  
  <p>I also know that it is possible to do with powermock, but I really like spock, as I find it incredibly lightweight and so was looking for a solution using spock.</p>
  
  <p>I thought something like this might have worked:</p>
  
  <pre><code>    def "An unexpected type of foo causes an exception to be thrown"() {
          given:
          Foo foo = Mock()
          when:
          subjectUnderTest.handleFoo foo
          then:
          thrown Exception
  }
  </code></pre>
  
  <p>However, this fails with the following error message:
  <code>org.spockframework.mock.CannotCreateMockException: Cannot create mock for class com.Foo because Java mocks cannot mock final classes. If the code under test is written in Groovy, use a Groovy mock.</code></p>
  
  <p>I was wondering if anybody knew of a way to do this using spock, as I can't find a solution on any documentation.</p>
  
  <p><strong>Edit</strong> After some of the comments below, I feel it is best to clarify why I want to write these tests.</p>
  
  <p>I work in a team with quite a few developers, and it is quite possible that one of them will update the enum. I want tests to fail if this happens to make the developer aware that they need to add logic to handle the new enum type. 
  For this, I wrote a test that iterates over every possible value that the enum could have, passed it into the method and verify that no exception is thrown. Now, if a user added a new enum, this test would fail as there is nothing to handle it so an exception would (hopefully) be thrown.</p>
  
  <p>The second test (the one I am struggling to write) is to clarify that the default logic works as I would expect it to, and that an exception does in fact get thrown. The only way I can think of doing this without creating an enum value that I will never want to be used is to mock an enum, and test that an exception gets thrown if the enum value is not handled.</p>
  
  <p>The code looks like this:</p>
  
  <p>Enum Foo:</p>
  
  <pre><code>public enum Foo {
      ONE, TWO;
  }
  </code></pre>
  
  <p>Method to handle foo:</p>
  
  <pre><code>private void handleFoo(Foo foo) {
      switch (foo) {
          case ONE:
             doEventOne();
             break;
          case TWO:
             doEventTwo()
             break;
          default:
             throw new IllegalArgumentException("Do not know how to handle " + foo);
  }
  </code></pre>