如何从JUnit启动Google App Engine Java开发服务器?

时间:2016-04-07 20:59:03

标签: java google-app-engine testing

我想编写针对本地开发Web服务器运行的自动化测试。编写针对底层服务(https://cloud.google.com/appengine/docs/java/tools/localunittesting)的测试很容易,但我想测试我的完整堆栈。我可以使用Runtime.exec()启动并终止服务器,但我希望有人开发出更优雅的解决方案。

1 个答案:

答案 0 :(得分:1)

我在the Javadocs找到了答案。使用DevAppServerTestRunner

@RunWith(DevAppServerTestRunner.class)
@DevAppServerTest(EndToEndTest.TestConfig.class)
public class EndToEndTest {

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

  public static class TestConfig extends BaseDevAppServerTestConfig {

    public File getSdkRoot() {
      return new File(...);
    }

    public File getAppDir() {
      return new File(...);
    }

    public List<URL> getClasspath() {
      return Arrays.asList(...);
    }
  }

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

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

  @Test
  public void testEndToEnd() throws Exception {
    URLFetchService fetchService = URLFetchServiceFactory.getURLFetchService();
    HTTPResponse resp = fetchService.fetch(new URL("http://localhost:8080/insertFoo?id=33"));
    Assert.assertEquals(200, resp.getResponseCode());
    DatastoreServiceFactory.getDatastoreService().get(KeyFactory.createKey("foo", 33));
  }
}