1
我想編寫針對本地開發Web服務器運行的自動化測試。對底層服務編寫測試非常簡單(https://cloud.google.com/appengine/docs/java/tools/localunittesting),但我想測試我的完整堆棧。我可以使用Runtime.exec()啓動並殺死服務器,但我希望有人開發出更優雅的解決方案。如何從JUnit啓動Google App Engine Java開發服務器?
我想編寫針對本地開發Web服務器運行的自動化測試。對底層服務編寫測試非常簡單(https://cloud.google.com/appengine/docs/java/tools/localunittesting),但我想測試我的完整堆棧。我可以使用Runtime.exec()啓動並殺死服務器,但我希望有人開發出更優雅的解決方案。如何從JUnit啓動Google App Engine Java開發服務器?
我找到了答案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));
}
}