java - 如何基于 Google Cloud Endpoints 为 API 编写测试?

标签 java google-app-engine maven testing google-cloud-endpoints

是否有某种方法可以使用 JUnit 或其他框架测试使用 Google Cloud Endpoint 编写的 API?

在文档中有一个使用 curl 命令的示例,这背后的逻辑可能是仅在客户端测试 API。

当我试图找到一些如何从服务器端测试 API 的方法时,我遇到了编写 JUnit 测试并将 HttpURLConnection 调用到本地主机的可能性,但这种方法存在问题.例如,app engine 的实例应该在测试之前就已经在运行了,但是我在本地部署了 maven 并且测试是在部署之前进行的,所以如果我的测试失败,它就不会部署开发服务器,我觉得那是不对的重写 Maven 步骤的方法。

编辑 1: 为 Python 找到了类似的东西:How to unit test Google Cloud Endpoints

最佳答案

使用 Objectify,您可以这样做。例如,让我们声明我们的 BooksEndpoint 如下:

@Api(
    name = "books",
    version = "v1",
    namespace = @ApiNamespace(ownerDomain = "backend.example.com", ownerName = "backend.example.com", packagePath = "")
)
public class BooksEndpoint {

    @ApiMethod(name = "saveBook")
    public void saveBook(Book book, User user) throws OAuthRequestException, IOException {
        if (user == null) {
            throw new OAuthRequestException("User is not authorized");
        }

        Account account = AccountService.create().getAccount(user);

        ofy().save()
                .entity(BookRecord.fromBook(account, book))
                .now();
    }

}

要测试它,您需要以下依赖项:

testCompile 'com.google.appengine:appengine-api-labs:1.9.8'
testCompile 'com.google.appengine:appengine-api-stubs:1.9.8'
testCompile 'com.google.appengine:appengine-testing:1.9.8'
testCompile 'junit:junit:4.12'

现在,测试将如下所示:

public class BooksEndpointTest {

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

    private Closeable objectifyService;

    @Before
    public void setUp() throws Exception {
        testHelper.setUp();
        objectifyService = ObjectifyService.begin(); // required if you want to use Objectify
    }

    @After
    public void tearDown() throws Exception {
        testHelper.tearDown();
        objectifyService.close();
    }

    @Test
    public void testSaveBook() throws Exception {
        // Create Endpoint and execute your method
        new BooksEndpoint().saveBook(
                Book.create("id", "name", "author"),
                new User("example@example.com", "authDomain")
        );

        // Check what was written into datastore
        BookRecord bookRecord = ofy().load().type(BookRecord.class).first().now();

        // Assert that it is correct (simplified)
        assertEquals("name", bookRecord.getName());
    }

}

请注意,我在这里使用 BookRecordBook - 它们是我的实体和 POJO,没有什么特别的。

关于java - 如何基于 Google Cloud Endpoints 为 API 编写测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23057678/

相关文章:

java - Apache POI : Excel changes my format string

java - 如何根据条件禁用 TestNG 测试

javascript - 用于 Google App Engine channel API 客户端的无窗口容器

maven - 如何在另一个项目中向Spring Boot Jar添加依赖项?

java - Maven 尝试再次下载 ojdbc14.jar - jar 已经存在于本地存储库中

maven - 如何为多模块项目中的特定模块激活 Maven 配置文件

java - 如何使用 Google Guava 查找某个元素,无论它是否存在于列表中?

java - 给定两个比较器对数组进行排序?

java - 如何使用java在谷歌应用程序引擎中创建docx文件

python - 建模谷歌数据存储/python