title: Mock Access to Outer Object From Local Inner Class
body: <p>I have a class with a local inner class in one of its methods:</p>
  
  <pre><code>public class Outer {
      String hello = "hello";
  
      public void myMethod() {
  
          class Inner {
              public void myInnerMethod() {
                  System.out.println(hello);         
              }
          }
  
          [...really slow routine...]
  
          (new Inner()).myInnerMethod();
      }
  }
  </code></pre>
  
  <p>I would like to test the <code>myInnerMethod()</code>. So I instantiate the local inner class using reflection and call the <code>myInnerMethod()</code> on it.</p>
  
  <pre><code>public void test() {
      Object inner = Class.forName("Outer$Inner").newInstance();
      inner.getClass().getDeclaredMethod("myInnerMethod").invoke(inner); // hello will be null
  }
  </code></pre>
  
  <p>But when <code>myInnerMethod()</code> accesses <code>hello</code>, which is in the scope of the Outer class, it is <code>null</code>.</p>
  
  <p>Is there a way to mock or otherwise provide hello to <code>myInnerMethod()</code>?</p>
  
  <p>I know I could refactor my code by extracting the inner class or just test the public methods of Outer. But is there still a way to do it?</p>