Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 3 years ago.
Improve this questionI'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)
How exactly would I go about 开发者_运维知识库doing this? I will be using JUnit as that's what I'm most familiar with.
Thanks in advance for any help.
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 business classes to take directly the AmazonS3 interface, then for unit testing you have to mock that one.
The actual exploratory testing (learning and verifying) if and how amazon s3 works is what you actually do in separate experimental setup.
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.
I'm not a Java programmer but you probably want to look into mocking. There is a SoapUI tool called MockService that appears to allow mocking of an external service like those provided by AWS.
Op De Cirkel answer is good in unit testing scope but if you are writing framework support or simply need to run the AWS S3 calls during your tests, you can run any service that offer AWS compatible APIs. OpenStack is one of them and can be run in a virtual machine (see DevStack).
Or you can choose from a variety of test-oriented tools that provide AWS compatible APIs.
Here are some that expose S3 service:
- S3Ninja (actually written in Java)
- FakeS3 (ruby)
- s3mock (Scala with Java API)
You could take a look at LocalStack, a framework that spins up a fully functional local cloud environment for integration testing.
LocalStack provides a subset of the AWS cloud services, including S3, Kinesis, Lambda, DynamoDB, and more.
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.
http://docs.aws.amazon.com/AWSJavaSDK/latest/javadoc/com/amazonaws/services/ec2/model/DryRunResult.html
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.
You can find more information about "Stand-alone Server Mode" at the end of the README.
精彩评论