1
我已經爲谷歌應用程序引擎編寫驗收測試。但每當我運行一個測試我得到以下異常谷歌應用程序引擎數據存儲測試沒有api環境註冊此線程的異常
java.lang.NullPointerException: No API environment is registered for this thread.
at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppId(DatastoreApiHelper.java:180)
at com.google.appengine.api.datastore.DatastoreApiHelper.getCurrentAppIdNamespace(DatastoreApiHelper.java:196)
at com.google.appengine.api.datastore.Query.<init>(Query.java:208)
at com.google.appengine.api.datastore.Query.<init>(Query.java:139)
at com.google.appengine.datanucleus.query.DatastoreQuery.compile(DatastoreQuery.java:236)
at com.google.appengine.datanucleus.query.JDOQLQuery.performExecute(JDOQLQuery.java:158)
at org.datanucleus.store.query.Query.executeQuery(Query.java:1789)
at org.datanucleus.store.query.Query.executeWithArray(Query.java:1666)
at org.datanucleus.store.query.Query.execute(Query.java:1639)
at org.datanucleus.api.jdo.JDOQuery.execute(JDOQuery.java:221)
at com.teamsuccesso.campaignmonitor.dao.impl.SimpleQueryDaoImpl.getObjectListUsingQuery(SimpleQueryDaoImpl.java:189)
at com.teamsuccesso.campaignmonitor.service.user.impl.UserServiceImpl.saveUser(UserServiceImpl.java:18)
at com.teamsuccesso.campaignmonitor.controller.user.UserController.createAdmin(UserController.java:62)
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 org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleElement.invoke(InitDestroyAnnotationBeanPostProcessor.java:349)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor$LifecycleMetadata.invokeInitMethods(InitDestroyAnnotationBeanPostProcessor.java:300)
at org.springframework.beans.factory.annotation.InitDestroyAnnotationBeanPostProcessor.postProcessBeforeInitialization(InitDestroyAnnotationBeanPostProcessor.java:133)
... 43 more
以下是我的代碼
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextHierarchy({
@ContextConfiguration(locations = { "file:src/main/webapp/WEB-INF/spring/dispatcher-servlet.xml" }) })
public class AT_AdGroupController {
@SuppressWarnings("unused")
private MockHttpSession session = new MockHttpSession();
private final LocalServiceTestHelper helper = new LocalServiceTestHelper(
new LocalDatastoreServiceTestConfig().setDefaultHighRepJobPolicyUnappliedJobPercentage(1).setStoreDelayMs(0));
@Autowired
private WebApplicationContext webContext;
private MockMvc mockMVC;
@SuppressWarnings("unused")
private static ObjectMapper mapper;
@BeforeClass
public static void startUp() {
mapper = new ObjectMapper();
}
@Before
public void testSetup() {
MockitoAnnotations.initMocks(this);
helper.setUp();
this.mockMVC = MockMvcBuilders.webAppContextSetup(webContext).build();
}
@Test
public void testSaveAd() throws Exception {
CommonAdBean ad = new CommonAdBean();
ad.setAdType("FaceBook");
ad.setAdName("AdName");
ad.setDateOfReport("Mon Oct 17 2016 17:26:36 GMT+0530 (India Standard Time)");
ad.setReportingStarts("1991-01-01");
ad.setTreportingEnds("1991-01-22");
ad.setStarts("1991-10-11");
ad.setEnds("01-01-1991");
String inputJSon = new GsonBuilder().serializeNulls().create().toJson(ad);
System.out.println("InPut:" + inputJSon);
MvcResult returnValue = this.mockMVC
.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post("/adgroups/1")
.contentType(MediaType.APPLICATION_JSON).content(inputJSon.getBytes()))
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status().isOk())
.andReturn();
System.out.println("OutPut:" + returnValue.getResponse().getContentAsString());
returnValue = this.mockMVC
.perform(org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post("/adgroups/1")
.contentType(MediaType.APPLICATION_JSON).content(inputJSon.getBytes()))
.andExpect(org.springframework.test.web.servlet.result.MockMvcResultMatchers.status().isOk())
.andReturn();
System.out.println("OutPut:" + returnValue.getResponse().getContentAsString());
AdBean adbean = new GsonBuilder().serializeNulls().create().fromJson(returnValue.getResponse().getContentAsString(), AdBean.class);
assertNotNull(adbean.getId());
}
}
對於依賴我使用Maven和我已經添加了所需的所有的依賴關係。以下是我加
- 的AppEngine-API-1.0-SDK
- AppEngine上,測試
- 的AppEngine-API存根
- 的AppEngine-API實驗室
- 的AppEngine工具,SDK的依賴性
我的AppEngine的版本是44年9月1日,我還使用Spring MVC的
您是否正在靈活的環境中運行? – chaiyachaiya