title: IllegalAccessError when accessing package private class in JMock
body: <p>I have written a unit test using JMock 2.6.0-RC2 that makes use of several package private classes. For some reason, this causes an <code>IllegalAccessError</code>:</p>
  
  <pre><code>java.lang.IllegalAccessError: tried to access class foo.PackagePrivateClass from class $Proxy6
      at $Proxy6.getInstance(Unknown Source)
      at foo.UsingClass.&lt;init&gt;(UsingClass.java:6)
      at foo.FailingTest.testFailure(FailingTest.java:29)
  </code></pre>
  
  <p>I have uploaded a minimal Maven project that demonstrates the issue to here: <a href="http://dl.dropbox.com/u/108474287/example-project.zip" rel="nofollow">http://dl.dropbox.com/u/108474287/example-project.zip</a>. The relevant code is also shown below (it's fairly concise). </p>
  
  <p>Why am I seeing an error in my test? The test class is part of the same package as the tested classes, so I would not expect access control to be an issue.</p>
  
  <p>Some interesting observations:</p>
  
  <ol>
  <li><p>If I change <code>ExampleInterface</code> to be package private (rather than public), the problem goes away. Sadly, in my real-life project this is not a possibility.</p></li>
  <li><p>The exception only occurs if I return a value from my mock object. Those who download my example project will see another test in which <code>null</code> is returned; this test passes.</p></li>
  <li><p>If I move the all the classes into the default package, the tests pass! If all the classes remain in package <code>foo</code>, the tests fail.</p></li>
  </ol>
  
  <p>Below are the files contained in my project. The ZIP I linked to above is a small Maven project that houses all of these.</p>
  
  <p><em>I have also posted this question to the JMock developer list. I will update this question if I receives answers there (and vice versa).</em></p>
  
  <hr>
  
  <p><strong>FailingTest.java</strong></p>
  
  <pre><code>package foo;
  
  // Imports omitted
  
  @RunWith(JMock.class)
  public class FailingTest {
  
    private static final PackagePrivateClass EXAMPLE_INSTANCE = 
        new PackagePrivateClass();
  
    public Mockery context = new JUnit4Mockery();
  
    @Test
    public void testFailure() {
      final ExampleInterface exampleInterface = context
          .mock(ExampleInterface.class);
  
      context.checking(new Expectations() {
        {
          oneOf(exampleInterface).getInstance(); 
          will(returnValue(EXAMPLE_INSTANCE));
        }
      });
  
      new UsingClass(exampleInterface); &lt;-- exception thrown from constructor
    }
  }
  </code></pre>
  
  <p><strong>ExampleInterface.java</strong></p>
  
  <pre><code>package foo;
  
  public interface ExampleInterface {
    PackagePrivateClass getInstance();
  }
  </code></pre>
  
  <p><strong>PackagePrivateClass.java</strong></p>
  
  <pre><code>package foo;
  
  class PackagePrivateClass {
  
    PackagePrivateClass() {}
  
    @Override
    public String toString() {
      return "Hello, World!";
    }
  }
  </code></pre>
  
  <p><strong>UsingClass.java</strong></p>
  
  <pre><code>package foo;
  
  class UsingClass {
  
    UsingClass(ExampleInterface exampleInterface) {
      PackagePrivateClass bar = exampleInterface.getInstance(); // &lt;--- exception
      System.out.println(bar);                                  // originates here
    }
  }
  </code></pre>