谷歌应用程序引擎集成测试(JAVA)(Integration tests for Google Ap

2019-07-30 17:12发布

我想开发我的GAE / J应用一些有效的集成测试。 我熟悉https://developers.google.com/appengine/docs/java/tools/localunittesting -这些工具都是伟大的小单元测试。 我现在感兴趣的开发测试实际的Web请求集成测试。 例如,我想测试的web.xml是映射servlet和过滤器,以预期的URL和测试我的JSP生成我的期望。

我的目标是弹出JVM,这我能火逆请求内的本地开发服务器。 我开放给其他一体化战略,虽然; 正如我上面说的,我只是想有效地测试JSP生成等请求级的功能。

我已经成功地使用DevAppServerFactory在同一个JVM启动开发服务器。 然而,看来这产生DevAppServer使用来自主JVM单独的类加载器。 这使得测试了很多更具挑战性的 - 我不能使用任何本地单元测试本地* TestConfig类来控制该服务器的行为。 同样,我也不能“推出自己的”钩子通过例如静态修改的行为,因为我可以在测试工具的突变静不是DevAppServer正在以相同的静。 这使得它具有挑战性的跳跃不是中央对当前的测试功能(例如要求登录),注入故障,注入嘲笑等,这确实限制如何全面有效我可以测试我的代码。

我发现文档的真正缺乏网络与App Engine的集成测试上。 我敢肯定有人之前......这样做是否有任何提示或资源,在那里你可以分享?

Answer 1:

基本上,你需要做两件事情:

  1. 添加两个servlet(或其他),必须在测试期间才启用 ,它允许你远程调用的建立和拆除的帮手。
  2. 让你的servlet引擎服务于一个完全单线程方式的请求。 这是必要的,因为某种原因,辅助类谷歌提供只需要在当前线程的效果。


Answer 2:

我同意这个问题是不良记录。
我设法写一个终端到终端的测试启动服务器作为一个黑盒子,并将其发送HTTP请求。

它的工作原理是这样的:

package com.project.org;

import static org.junit.Assert.assertEquals;
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.api.urlfetch.HTTPResponse;
import com.google.appengine.api.urlfetch.URLFetchServiceFactory;
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.LocalServiceTestHelper;

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

  public class TestConfig extends BaseDevAppServerTestConfig {

    @Override public File getSdkRoot() {
      // You may need to tweak this.
      return new File("../../appengine-java-sdk-1.9.15");
    }

    @Override public File getAppDir() {
      return new File("war");
    }

    @Override
    public List<URL> getClasspath() {
      // There may be an easier way to do this.
      List<URL> classPath = new ArrayList<>();
      try {
        String separator = System.getProperty("path.separator");
        String[] pathElements = System.getProperty("java.class.path").split(separator);
        for (String pathElement : pathElements) {
          classPath.add(new File(pathElement).toURI().toURL());
        }
      }
      catch (MalformedURLException e) {
        throw new RuntimeException(e);
      }
      return classPath;
    }
  }

  private final LocalServiceTestHelper testHelper;
  private final String port;

  public HelloWorldTest() {
    testHelper = new LocalServiceTestHelper();
    port = System.getProperty("appengine.devappserver.test.port");
  }

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

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

  @Test public void testHelloWorld() throws Exception {
    URL url = new URL("http://localhost:" + port + "/hello");
    HTTPResponse response = URLFetchServiceFactory.getURLFetchService().fetch(url);
    assertEquals(200, response.getResponseCode());
    assertEquals("Hello world!", new String(response.getContent(), "UTF-8"));
  }
}

现在我有问题是,如果你有两个那些测试,每个单独路过,你不能在相同的二进制运行它们。 对37线的异常此文件抛出:

IllegalStateException异常(“开发应用程序服务器已在运行。”)

不知道如何解决这个问题。



文章来源: Integration tests for Google App Engine (java)