3

我很难让 Jersey、AppEngine 和 JUnit 工作。当前错误如下:

没有为此线程注册 API 环境。

我正在做以下事情:

  • 通过 JUnit @Before 注解加载 helper.setUp() (LocalDataStoreService)

  • 通过 JUnit @Before 注解创建 Grizzly Webserver 的实例

  • 执行 testCreateCard 测试

  • 停止 Grizzly 网络服务器

  • 在 JUnit @After 注解上运行 helper.tearDown() 方法

我使用 Eclipse Indigo 启动(运行方式)Junit 测试(我在 Java BuildPath 上包含了 jar)。

似乎 Jersey 或 Grizzly Webcontainer 正在打开不适用于本地数据存储方法 helper.setUp() 和 helper.tearDown() 的新线程。

我发现了一些问题/答案,但与将 AppEngine 与 Jersey 结合使用无关。

有没有人可以帮忙?Jersey 和 AppEngine 和 JUnit 的设置真的可行吗?我用rest-assured和jersey-test-framework尝试了几种可能性,但所有尝试都以错误和调试告终,并且在AppEngine Mailinglist中发帖也没有任何结果。

我最后的希望是我目前的设置:

  • Grizzly 2.1(泽西岛提供)

  • 泽西 1.12

  • JUnit 4.10

堆栈跟踪如下:

java.lang.NullPointerException: No API environment is registered for this thread.
    at  com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:86)
at     com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppIdNamespace(DatastoreApiHelper.java:96)
at com.google.appengine.api.datastore.Key.<init>(Key.java:106)
at com.google.appengine.api.datastore.Key.<init>(Key.java:90)
at com.google.appengine.api.datastore.Key.<init>(Key.java:86)
at com.google.appengine.api.datastore.Entity.<init>(Entity.java:125)
at com.google.appengine.api.datastore.Entity.<init>(Entity.java:106)
at org.datanucleus.store.appengine.DatastoreFieldManager.<init>(DatastoreFieldManager.java:189)
at  org.datanucleus.store.appengine.DatastorePersistenceHandler.insertPreProcess(DatastorePersistenceHandler.java:354)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObjects(DatastorePersistenceHandler.java:267)
at org.datanucleus.store.appengine.DatastorePersistenceHandler.insertObject(DatastorePersistenceHandler.java:256)
at org.datanucleus.state.JDOStateManagerImpl.internalMakePersistent(JDOStateManagerImpl.java:3185)
at org.datanucleus.state.JDOStateManagerImpl.makePersistent(JDOStateManagerImpl.java:3161)
at org.datanucleus.ObjectManagerImpl.persistObjectInternal(ObjectManagerImpl.java:1298)
at org.datanucleus.ObjectManagerImpl.persistObject(ObjectManagerImpl.java:1175)
at org.datanucleus.jdo.JDOPersistenceManager.jdoMakePersistent(JDOPersistenceManager.java:669)
at org.datanucleus.jdo.JDOPersistenceManager.makePersistent(JDOPersistenceManager.java:694)
at com.acolsolutions.loyaltycard.dataobjects.CardDAO.create(CardDAO.java:23)
at com.acolsolutions.loyaltycard.resources.CardResource.create(CardResource.java:32)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at com.sun.jersey.spi.container.JavaMethodInvokerFactory$1.invoke(JavaMethodInvokerFactory.java:60)
at com.sun.jersey.server.impl.model.method.dispatch.AbstractResourceMethodDispatchProvider$ResponseOutInvoker._dispatch(AbstractResourceMethodDispatchProvider.java:205)
at com.sun.jersey.server.impl.model.method.dispatch.ResourceJavaMethodDispatcher.dispatch(ResourceJavaMethodDispatcher.java:75)
at com.sun.jersey.server.impl.uri.rules.HttpMethodRule.accept(HttpMethodRule.java:288)
at com.sun.jersey.server.impl.uri.rules.ResourceClassRule.accept(ResourceClassRule.java:108)
at com.sun.jersey.server.impl.uri.rules.RightHandPathRule.accept(RightHandPathRule.java:147)
at com.sun.jersey.server.impl.uri.rules.RootResourceClassesRule.accept(RootResourceClassesRule.java:84)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1483)
at com.sun.jersey.server.impl.application.WebApplicationImpl._handleRequest(WebApplicationImpl.java:1414)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1363)
at com.sun.jersey.server.impl.application.WebApplicationImpl.handleRequest(WebApplicationImpl.java:1353)
at com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainer._service(GrizzlyContainer.java:215)
at com.sun.jersey.server.impl.container.grizzly2.GrizzlyContainer.service(GrizzlyContainer.java:185)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:163)
at org.glassfish.grizzly.http.server.HttpHandlerChain.service(HttpHandlerChain.java:207)
at org.glassfish.grizzly.http.server.HttpHandler.doHandle(HttpHandler.java:163)
at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:164)
at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134)
at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112)
at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:78)
at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:816)
at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:111)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55)
at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:566)
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:546)
at java.lang.Thread.run(Unknown Source)

我的 Jersey Test 课程如下所示:

import java.io.IOException;
import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import javax.ws.rs.core.UriBuilder;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;

import org.glassfish.grizzly.http.server.HttpServer;

import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.api.client.Client;

import org.codehaus.jettison.json.JSONObject;

public class CardResourceTests {
private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
        new LocalDatastoreServiceTestConfig()); 

private HttpServer httpServer;
private URI uri;

@Before
public void setUp() throws Exception {
    helper.setUp();

    uri = UriBuilder.fromUri("http://localhost/").port(9998).build();
    ResourceConfig rc = new PackagesResourceConfig("com.acolsolutions.loyaltycard.resources");
    httpServer = GrizzlyServerFactory.createHttpServer(uri, rc);
}

@After
public void tearDown() throws Exception {
    helper.tearDown();

    httpServer.stop();
}

@Test
public void testCreate() {
    boolean thrown = false;
    Client client = Client.create();
    WebResource webResource = client.resource(uri);
    JSONObject card = new JSONObject();

    try {
        card.put("id", "1")
        .put("name", "Name of Card")
        .put("code", "123456")
        .put("codeTypeId", "1")
        .put("cardProviderName", "The Card Provider")
        .put("picturePath", "provider.jpg")
        .put("cardProviderUrl", "http://www.provider.com")
        .put("creationDate", "Sun Jun 10 08:55:14 UTC 2012")
        .put("modificationDate","Sun Jun 10 08:55:14 UTC 2012");
    webResource.path("cards").type("application/json").post(card);
    } catch(Exception e) {
        e.printStackTrace();
        thrown = true;          
    }

    assertEquals("Result", false, thrown);
}
}
4

1 回答 1

3

如果您使用的是appengine,为什么要尝试使用grizzly?Appengine 是您的容器,而不是灰熊……

于 2012-07-10T14:33:36.030 回答