无法设置App Engine测试环境

时间:2016-05-30 15:01:01

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

我正在尝试对我的应用引擎端点api进行单元测试。我正在关注http://dev.mysql.com/doc/refman/5.7/en/cast-functions.html的例子。他们似乎正在使用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
  }
}

这是Endpoint测试类(为简明起见,省略了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变量的地方。

1 个答案:

答案 0 :(得分:0)

我遇到一个错误,但专门针对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>

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