title: Testing Google App Engine ThreadManager outside of GAE
body: <p>I've written a JUnit (4.10) unit test that makes the following call to <code>com.google.appengine.api.ThreadManager</code>:</p>
  
  <pre><code>ThreadManager.currentRequestThreadFactory();
  </code></pre>
  
  <p>When this test runs, I get a <code>NullPointerException</code> being thrown from within this <code>currentRequestThreadFactory</code> method:</p>
  
  <pre><code>Caused by: java.lang.NullPointerException
      at com.google.appengine.api.ThreadManager.currentRequestThreadFactory(ThreadManager.java:39)
      at com.myapp.server.plumbing.di.BaseModule.providesThreadFactory(BaseModule.java:50)
      at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
      at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
      at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
  </code></pre>
  
  <p>When I pull down the source for <code>ThreadManager</code>, and look at Line 39 (which is the source of the NPE), I see:</p>
  
  <pre><code>public static ThreadFactory currentRequestThreadFactory() {
          return (ThreadFactory) ApiProxy.getCurrentEnvironment().getAttributes()
              .get(REQUEST_THREAD_FACTORY_ATTR);
  }
  </code></pre>
  
  <p>So it seems that the <code>ApiProxy.getCurrentEnvironment()</code> is null, and when it's <code>getAttribute()</code> method is called, the NPE is thrown. I've confirmed this by adding some new print statements higher up in my unit test code:</p>
  
  <pre><code>if(ApiProxy.getCurrentEnvironment() == null)
      System.out.println("Environment is null.");
  </code></pre>
  
  <p>I'm vaguely aware that GAE offers "test versions" for all its services, but haven't been able to find (specifically) how to use them and set them up. So I ask: does GAE offer such test versions? If so, how do I add an <code>ApiProxy</code> test version here? And if not, then what are my options? I don't think I can mock either method (<code>ThreadManager#currentRequestThreadFactory</code> or <code>ApiProxy#getCurrentEnvironment</code>)  because they're both statics. Thanks in advance.</p>
  
  <p><strong>Edit</strong>: I see that there is an <code>appengine-testing.jar</code> that ships with the SDK. Inside this JAR is an <code>ApiProxyLocal.class</code> that I <em>believe</em> is a version of <code>ApiProxy</code> that could be used during JUnit testing, that would work without throwing the NPE. If that's the case (which I'm not even sure of), then the question is: how do I inject it into my <code>ThreadManager</code> for this test?</p>