java - 无法设置App Engine测试环境

标签 java android unit-testing google-app-engine gradle

我正在尝试对我的应用程序引擎终结点api进行单元测试。我正在遵循udacity中的示例。他们似乎在使用一个LocalServiceTestHelper,但我很难理解为什么,因为代码后面没有使用它。
这是我的后台gradle文件:

buildscript {
    repositories {
        jcenter()
    }
    dependencies {
        classpath 'com.google.appengine:gradle-appengine-plugin:1.9.28'
    }
}

repositories {
    jcenter();
}

apply plugin: 'java'
apply plugin: 'war'
apply plugin: 'appengine'

sourceCompatibility = JavaVersion.VERSION_1_7
targetCompatibility = JavaVersion.VERSION_1_7

dependencies {
  appengineSdk 'com.google.appengine:appengine-java-sdk:1.9.28'
  compile 'com.google.appengine:appengine-endpoints:1.9.28'
  compile 'com.google.appengine:appengine-endpoints-deps:1.9.28'
  compile 'javax.servlet:servlet-api:2.5'
    compile 'com.googlecode.objectify:objectify:5.0.3'
    compile 'javax.jdo:jdo-api:3.0.1'
    compile 'junit:junit:4.12'
    compile 'com.google.appengine:appengine-testing:1.9.24'
}

appengine {
  downloadSdk = true
  appcfg {
    oauth2 = true
  }
  endpoints {
    getClientLibsOnBuild = true
    getDiscoveryDocsOnBuild = true
  }
}

下面是端点测试类(为了简洁起见,省略了import语句和其他琐碎的细节):
private final LocalServiceTestHelper helper =
            new LocalServiceTestHelper(new LocalDatastoreServiceTestConfig()
                    .setDefaultHighRepJobPolicyUnappliedJobPercentage(100));

    /**
     * Set up the local service tester before running tests and an instance of the magpieApi.
     * @throws Exception
     */
    @Before
    public void setUp() throws Exception {
        //helper.setUp();
        user = new User(EMAIL, "gmail.com" ,USER_ID);
        magpieApi = new MagpieEndpoint();
    }

    /**
     * After running tests clear objectify and tear down the local test helper.
     * @throws Exception
     */
    @After
    public void tearDown() throws Exception {
        ofy().clear();
        helper.tearDown();
    }

以下是错误日志的重要部分:
java.lang.NoClassDefFoundError: com/google/appengine/api/datastore/dev/LocalDatastoreService$AutoIdAllocationPolicy

    at com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig.<init>(LocalDatastoreServiceTestConfig.java:24)
    at test.MagpieEndpointTest.<init>(MagpieEndpointTest.java:54)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
    at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
    at org.junit.runners.BlockJUnit4ClassRunner.createTest(BlockJUnit4ClassRunner.java:217)
    at org.junit.runners.BlockJUnit4ClassRunner$1.runReflectiveCall(BlockJUnit4ClassRunner.java:266)
    at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12)
    at org.junit.runners.BlockJUnit4ClassRunner.methodBlock(BlockJUnit4ClassRunner.java:263)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:78)
    at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:57)
    at org.junit.runners.ParentRunner$3.run(ParentRunner.java:290)
    at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:71)
    at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:288)
    at org.junit.runners.ParentRunner.access$000(ParentRunner.java:58)
    at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:268)
    at org.junit.runners.ParentRunner.run(ParentRunner.java:363)
    at org.junit.runner.JUnitCore.run(JUnitCore.java:137)
    at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69)
    at com.intellij.rt.execution.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:234)
    at com.intellij.rt.execution.junit.JUnitStarter.main(JUnitStarter.java:74)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:144)
Caused by: java.lang.ClassNotFoundException: com.google.appengine.api.datastore.dev.LocalDatastoreService$AutoIdAllocationPolicy

我是谷歌应用程序引擎新手,以前从未对我的代码进行过单元测试。没有语法错误,所以我很好奇是什么导致了这个问题。第54行显然是问题的根源,在这里我启动并声明helper变量。

最佳答案

我有一个错误,但特别是针对localservicetesthelper,当我在pom.xml中添加google测试依赖项时,这个错误就消失了,也许可以尝试一下。

<!-- https://mvnrepository.com/artifact/com.google.appengine/appengine-testing -->
<dependency>
    <groupId>com.google.appengine</groupId>
    <artifactId>appengine-testing</artifactId>
    <version>1.9.67</version>
    <scope>test</scope>
</dependency>

当然,使用任何合适的版本。

关于java - 无法设置App Engine测试环境,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37528721/

相关文章:

java - libgdx - gameoverscreen 不会显示

java - 此类上获取线程已启动错误?

java - 使用 byte-buddy 从字符串重写类方法?

java - 如何对部署在 Tomcat 上的 Jersey Web 应用程序进行单元测试?

java - MyBatis 在一个查询中删除-插入-更新

java - 如何使用密码和 Java 将 12 位十进制数字加密/解密为其他数字?

java - 多个类中可以访问同一个LinkedList吗?

javascript - 无法在移动设备中通过 jquery 暂停视频

javascript - Backbone.js 解析方法

ios - 测试在 swift 中调用函数的定时器