java 您如何测试针对 AWS API 编写的代码
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6247018/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you test code written against AWS API
提问by BrianJakovich
I'm writing an application in Java that will upload a file up to AWS S3. The file will be given to the application in an argument, not hardcoded. I'd like to write tests to ensure that the file actually gets uploaded to S3. The test will be written before the code for TDD. (I have actually already written the code, but I'd like to ingrain TDD practices into all of my work as a habit)
我正在用 Java 编写一个应用程序,它将文件上传到 AWS S3。该文件将在参数中提供给应用程序,而不是硬编码。我想编写测试以确保文件实际上传到 S3。测试将在 TDD 代码之前编写。(我实际上已经编写了代码,但我希望将 TDD 实践作为一种习惯融入我的所有工作中)
How exactly would I go about doing this? I will be using JUnit as that's what I'm most familiar with.
我究竟会怎么做呢?我将使用 JUnit,因为这是我最熟悉的。
Thanks in advance for any help.
在此先感谢您的帮助。
回答by Op De Cirkel
The actual uploading and the tests that are doing it are part of your integration testing, not the unit testing. If you wrap the S3 API in a very thin class, you will mock that class for unit testing of your business classes, and you will use the real implementation for integration testing.
If you have decided, your businessclasses to take directly the AmazonS3 interface, then for unit testing you have to mock that one.
实际上传和正在执行的测试是集成测试的一部分,而不是单元测试。如果您将 S3 API 包装在一个非常瘦的类中,您将模拟该类以对您的业务类进行单元测试,并且您将使用真正的实现进行集成测试。
如果您已经决定,您的 业务类直接采用AmazonS3 接口,那么对于单元测试,您必须模拟那个接口。
The actual exploratory testing(learning and verifying) if and how amazon s3 works is what you actually do in separate experimental setup.
实际的探索性测试(学习和验证)是否以及如何 amazon s3 工作是您在单独的实验设置中实际执行的操作。
P.S. I do not recommend using the AmazonS3 interface directly in your business classes, rather, wrap it in a thin interface of yours, so that if you decide to change the 'back-end storage' you can easily change it.
PS 我不建议直接在您的业务类中使用 AmazonS3 接口,而是将其包装在您的瘦接口中,以便如果您决定更改“后端存储”,您可以轻松更改它。
回答by Brian Lyttle
I'm not a Java programmer but you probably want to look into mocking. There is a SoapUI tool called MockServicethat appears to allow mocking of an external service like those provided by AWS.
我不是 Java 程序员,但您可能想研究mocking。有一个名为MockService的 SoapUI 工具似乎允许模拟 AWS 提供的外部服务。
回答by eskatos
Op De Cirkel answer is good in unit testing scope but if you are writing framework support or simply needto run the AWS S3 calls during your tests, you can run any service that offer AWS compatible APIs. OpenStackis one of them and can be run in a virtual machine (see DevStack).
Op De Cirkel 的答案在单元测试范围内很好,但如果您正在编写框架支持或仅需要在测试期间运行 AWS S3 调用,您可以运行任何提供 AWS 兼容 API 的服务。OpenStack就是其中之一,可以在虚拟机中运行(参见DevStack)。
Or you can choose from a variety of test-oriented tools that provide AWS compatible APIs.
或者,您可以从提供 AWS 兼容 API 的各种面向测试的工具中进行选择。
Here are some that expose S3 service:
以下是一些公开 S3 服务的内容:
回答by whummer
You could take a look at LocalStack, a framework that spins up a fully functional local cloud environment for integration testing.
您可以查看LocalStack,这是一个框架,它可以启动功能齐全的本地云环境以进行集成测试。
LocalStack provides a subset of the AWS cloud services, including S3, Kinesis, Lambda, DynamoDB, and more.
LocalStack 提供了 AWS 云服务的一个子集,包括 S3、Kinesis、Lambda、DynamoDB 等。
回答by Hrishikesh
As suggested in above answer better approach be mock AWS API response. Another alternative to get the illusion of API action is, to invoke AWS SDK or API with dry run mode/parameter. But you may require internet access while executing the tests.
正如上面的答案所建议的,更好的方法是模拟 AWS API 响应。另一种获得 API 操作错觉的替代方法是使用试运行模式/参数调用 AWS 开发工具包或 API。但是在执行测试时您可能需要访问 Internet。
回答by igonzalez
The question was answered long time ago but I would like to comment another approach.
很久以前就回答了这个问题,但我想评论另一种方法。
During my testing in different Python projects I've been using this library. I've found it very useful, even if you're developing with other languages like Java or Scala because you can setup a background server and mock all the AWS calls.
在我对不同 Python 项目的测试期间,我一直在使用这个库。我发现它非常有用,即使您正在使用 Java 或 Scala 等其他语言进行开发,因为您可以设置后台服务器并模拟所有 AWS 调用。
You can find more information about "Stand-alone Server Mode" at the end of the README.
您可以在 README 的末尾找到有关“独立服务器模式”的更多信息。