简体   繁体   English

如何使用Google Drive API(Java客户端)对应用程序进行单元测试

[英]How to unit test an application using Google Drive API (Java client)

What is the best way to unit test an application using the Google Drive API (Java client) ? 使用Google Drive API(Java客户端)对应用程序进行单元测试的最佳方法是什么?

It seems like applications written rely heavily on the Drive class, but short of either... 似乎编写的应用程序严重依赖于Drive类,但缺少...

  • creating a really extensive mock (which, itself, would likely need to be tested), or 创建一个非常广泛的模拟(它本身可能需要测试),或
  • writing an integration test dependent on the actual Drive service 根据实际的Drive服务编写集成测试

...how could such an application be tested? ......如何测试这样的应用程序?

Using mock frameworks like Mockito are a bit tedious with the Drive API (Java client), since usage of the Drive Java client rely on making so many chained calls (eg, from the documentation): 使用像Mockito这样的模拟框架对于Drive API(Java客户端)来说有点乏味,因为Drive Java客户端的使用依赖于进行如此多的链式调用(例如,来自文档):

Drive service = getDriveService(req, resp);
service.files().get(fileId).execute();

It shouldn't be that tedious in Mockito in fact, with the help of deep stub: 事实上,在深层存根的帮助下,Mockito应该不那么乏味:

Drive mockDrive = mock(Drive.class, RETURNS_DEEP_STUBS);

....
// stubbing
when(service.files().get(anyString()).execute()).thenReturn(something);

// verify
verify(service.files().get("Some Field ID").execute();

Learn more from documentation of Mockito Mockito的文档中了解更多信息

It is fine if you write integration test to test against the actual Drive service, but it simply cannot replace unit testing. 如果您编写集成测试来测试实际的Drive服务,这很好,但它根本无法取代单元测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM