在junit中启动Google App Engine开发服务器

时间:2018-07-31 11:54:39

标签: java google-app-engine junit

我正在尝试从junit脚本中启动Google App Engine DevServer,以便收集服务器端代码(包括servlet层)的测试覆盖率统计信息。

我尝试使用DevAppServerTestRunner,但出现初始化错误,没有任何解释。代码转载如下。

package uk.me.mukherjee.swindle.test;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

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

import com.google.appengine.tools.development.testing.BaseDevAppServerTestConfig;
import com.google.appengine.tools.development.testing.DevAppServerTest;
import com.google.appengine.tools.development.testing.DevAppServerTestRunner;
import com.google.appengine.tools.development.testing.LocalDatastoreServiceTestConfig;
import com.google.appengine.tools.development.testing.LocalServiceTestHelper;
import com.google.appengine.tools.development.testing.LocalURLFetchServiceTestConfig;

@RunWith(DevAppServerTestRunner.class)
@DevAppServerTest(AppTest2.TestConfig.class)

public class AppTest2 {

    private final LocalServiceTestHelper testHelper = new LocalServiceTestHelper(new LocalURLFetchServiceTestConfig(),
            new LocalDatastoreServiceTestConfig());

    public static class TestConfig extends BaseDevAppServerTestConfig {

        public File getSdkRoot() {
            return new File(
                    "/Users/pmu/.m2/repository/com/google/appengine/appengine-java-sdk/1.9.64/appengine-java-sdk/appengine-java-sdk-1.9.64");
        }

        public File getAppDir() {
            return new File(System.getProperty("user.dir") + "/target/swindle-1.7-SNAPSHOT");
        }

        public List<URL> getClasspath() {
            List<URL> urls = new ArrayList<URL>();
            String classpathStr = System.getProperty("java.class.path");
            String[] classpaths = classpathStr.split(":");
            for (String c : classpaths) {
                File f = new File(c);
                try {
                    urls.add(f.toURL());
                } catch (MalformedURLException e) {
                    System.out.println(e.toString());
                }
            }

            return urls;

        }
    }

    @Before
    public void setUpHelper() {
        testHelper.setUp();
    }

    @After
    public void tearDownHelper() {
        testHelper.tearDown();
    }

    @Test
    public void myTest() {
        assertTrue(true);
    }

}

在maven中运行时,出现以下错误:

Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.284 sec <<< FAILURE!
initializationError(uk.me.mukherjee.swindle.test.AppTest2)  Time elapsed: 0.007 sec  <<< ERROR!
java.lang.RuntimeException: Unable to create a DevAppServer
        at com.google.appengine.tools.development.DevAppServerFactory.doCreateDevAppServer(DevAppServerFactory.java:401)
        at com.google.appengine.tools.development.DevAppServerFactory.access$000(DevAppServerFactory.java:31)
        at com.google.appengine.tools.development.DevAppServerFactory$1.run(DevAppServerFactory.java:318)
        at com.google.appengine.tools.development.DevAppServerFactory$1.run(DevAppServerFactory.java:315)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.google.appengine.tools.development.DevAppServerFactory.createDevAppServer(DevAppServerFactory.java:314)
        at com.google.appengine.tools.development.DevAppServerFactory.createDevAppServer(DevAppServerFactory.java:266)
        at com.google.appengine.tools.development.testing.DevAppServerTestHelper.startServer(DevAppServerTestHelper.java:62)
        at com.google.appengine.tools.development.testing.DevAppServerTestRunner.startServerAndIsolateClass(DevAppServerTestRunner.java:86)
        at com.google.appengine.tools.development.testing.DevAppServerTestRunner.<init>(DevAppServerTestRunner.java:74)
        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.internal.builders.AnnotatedBuilder.buildRunner(AnnotatedBuilder.java:29)
        at org.junit.internal.builders.AnnotatedBuilder.runnerForClass(AnnotatedBuilder.java:21)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
        at org.junit.internal.builders.AllDefaultPossibilitiesBuilder.runnerForClass(AllDefaultPossibilitiesBuilder.java:26)
        at org.junit.runners.model.RunnerBuilder.safeRunnerForClass(RunnerBuilder.java:59)
        at org.junit.internal.requests.ClassRequest.getRunner(ClassRequest.java:26)
        at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:250)
        at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
        at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
        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 org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
        at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
        at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
        at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
        at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
Caused by: java.lang.ExceptionInInitializerError
        at com.google.appengine.tools.development.DevAppServerImpl.<init>(DevAppServerImpl.java:127)
        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 com.google.appengine.tools.development.DevAppServerFactory.doCreateDevAppServer(DevAppServerFactory.java:386)
        ... 31 more
Caused by: java.lang.IllegalStateException: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessDeclaredMembers")
        at com.google.appengine.tools.development.DevSocketImplFactory.<clinit>(DevSocketImplFactory.java:74)
        ... 37 more
Caused by: java.security.AccessControlException: access denied ("java.lang.RuntimePermission" "accessDeclaredMembers")
        at java.security.AccessControlContext.checkPermission(AccessControlContext.java:472)
        at java.security.AccessController.checkPermission(AccessController.java:884)
        at java.lang.SecurityManager.checkPermission(SecurityManager.java:549)
        at java.lang.Class.checkMemberAccess(Class.java:2348)
        at java.lang.Class.getDeclaredConstructor(Class.java:2177)
        at com.google.appengine.tools.development.DevSocketImplFactory.<clinit>(DevSocketImplFactory.java:72)
        ... 37 more

最小pom.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <version>1.7-SNAPSHOT</version>
  <groupId>uk.me.mukherjee.swindle</groupId>
  <artifactId>swindle</artifactId>
  <properties>
    <appengine.version>1.9.64</appengine.version>
  </properties>
  <dependencies>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-api-1.0-sdk</artifactId>
      <version>${appengine.version}</version>
      <scope>compile</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-testing</artifactId>
      <version>${appengine.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>4.11</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-tools-sdk</artifactId>
      <version>${appengine.version}</version>
      <scope>test</scope>
    </dependency>
    <dependency>
      <groupId>com.google.appengine</groupId>
      <artifactId>appengine-agent-sdk</artifactId>
      <version>1.9.38</version>
      <scope>test</scope>
    </dependency>
  </dependencies>
  <build>
    <plugins>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <version>3.1</version>
        <artifactId>maven-compiler-plugin</artifactId>
        <configuration>
          <source>1.8</source>
          <target>1.8</target>
        </configuration>
      </plugin>
      <plugin>
        <groupId>org.apache.maven.plugins</groupId>
        <artifactId>maven-war-plugin</artifactId>
        <version>2.4</version>
      </plugin>
    </plugins>
  </build>
</project>

0 个答案:

没有答案