Slim3 での No API environment is registered for this thread.

Service クラスの Test Case を作成していてテストを実行するとエラーが発生する。

java.lang.NullPointerException: No API environment is registered for this thread.

調べてみると、

ただ1点だけ、テスト用に Key を生成する方法がわかりません。
無理やり Key を生成しようとすると、以下のエラーが出てしまいます。

http://d.hatena.ne.jp/hageyahhoo/20100206/1265449051

という記述をみつけた。
確かにテストケース内で、

public class ItemServiceTest extends AppEngineTestCase {
    private ItemService service = new ItemService();
    private Key key = Datastore.createKey(Item.class, "4873114926");

と key を生成していた。
解決方法も引用先に記載されていたが、1年前以上の記載だったため、今度はメソッド内で key を記述してみた。*1

public class ItemServiceTest extends AppEngineTestCase {
    private ItemService service = new ItemService();
    @Test
    public void test() throws Exception {
        Key key = Datastore.createKey(Item.class, "4873114926");

すると今度は、「java.lang.NullPointerException: No API environment is registered for this thread.」が発生せずに、テストを完了できた。

*1:実は最初は Test Case のメソッド内に記述をしており、その際は今回のエラーが発生しないことに思い出して解決することができた