-
Notifications
You must be signed in to change notification settings - Fork 104
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Java Testing example using Spock (#287)
* feat: spock testing example * docs: update description in metadata * docs: update example to spock * docs: update mock title * feat: move groovy tests to original project * move groovy tests to original project * chore: cleanup old project * docs: upgrade README.md description of spock * chore: update meta data
1 parent
7e5f96f
commit 3c21de2
Showing
7 changed files
with
184 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
37 changes: 37 additions & 0 deletions
37
java-test-samples/apigw-lambda-list-s3-buckets/src/test/groovy/com/example/AppSpecIT.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package com.example | ||
|
||
import com.amazonaws.xray.AWSXRay | ||
import groovy.json.JsonSlurper | ||
import spock.lang.Specification | ||
|
||
import static com.example.fixtures.ApiGwRequestFixtures.getRequestFromFile | ||
|
||
class AppSpecIT extends Specification { | ||
|
||
private static final String BAD_REQUEST_FILE = "events/apigw_req_s3_buckets_post.json" | ||
|
||
def app = new App() | ||
|
||
def "returns a list of buckets"() { | ||
when: "a request is received" | ||
def request = getRequestFromFile() | ||
//This line manually adds the X-ray segment | ||
AWSXRay.beginSegment("S3"); | ||
def responseEvent = app.handleRequest(request, null) | ||
|
||
then: "a list of buckets is returned" | ||
def responseBody = new JsonSlurper().parseText(responseEvent.getBody()) as List | ||
responseBody.size() >= 1 | ||
} | ||
|
||
def "method not supported response"() { | ||
when: "a request is received" | ||
def request = getRequestFromFile(BAD_REQUEST_FILE) | ||
|
||
AWSXRay.beginSegment("S3") | ||
def responseEvent = app.handleRequest(request, null) | ||
|
||
then: "a method not supported response is returned" | ||
responseEvent.getStatusCode() == 405 | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...t-samples/apigw-lambda-list-s3-buckets/src/test/groovy/com/example/AppWithMockSpec.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.example | ||
|
||
import groovy.json.JsonSlurper | ||
import software.amazon.awssdk.services.s3.S3Client | ||
import spock.lang.Specification | ||
|
||
import static com.example.fixtures.ApiGwRequestFixtures.getRequestFromFile | ||
import static com.example.fixtures.BucketFixtures.TEST_BUCKET_NAME | ||
import static com.example.fixtures.BucketFixtures.listWithBucket | ||
|
||
class AppWithMockSpec extends Specification { | ||
|
||
|
||
def mockS3Client = Mock(S3Client) | ||
def app = new App(mockS3Client) | ||
|
||
def "returns a list of buckets"() { | ||
given: "a bucket exists" | ||
1 * mockS3Client.listBuckets() >> listWithBucket() | ||
|
||
when: "a request is received" | ||
def request = getRequestFromFile() | ||
def responseEvent = app.handleRequest(request, null) | ||
|
||
then: "a list of buckets is returned" | ||
def responseBody = new JsonSlurper().parseText(responseEvent.getBody()) as List | ||
responseBody.size() >= 1 | ||
|
||
and: "the first item is the example bucket" | ||
responseBody.first() == TEST_BUCKET_NAME | ||
} | ||
} |
18 changes: 18 additions & 0 deletions
18
...w-lambda-list-s3-buckets/src/test/groovy/com/example/fixtures/ApiGwRequestFixtures.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package com.example.fixtures | ||
|
||
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent | ||
import com.fasterxml.jackson.databind.DeserializationFeature | ||
import com.fasterxml.jackson.databind.ObjectMapper | ||
|
||
class ApiGwRequestFixtures { | ||
|
||
private static String DEFAULT_REQUEST_PAYLOAD_PATH = "events/apigw_req_s3_buckets_get.json" | ||
private static String TEST_RESOURCES_PATH = "src/test/resources/" | ||
|
||
static def getRequestFromFile(String filePath = DEFAULT_REQUEST_PAYLOAD_PATH) { | ||
def file = new File(TEST_RESOURCES_PATH + filePath) | ||
return new ObjectMapper() | ||
.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES) | ||
.readValue(file, APIGatewayProxyRequestEvent) | ||
} | ||
} |
22 changes: 22 additions & 0 deletions
22
...s/apigw-lambda-list-s3-buckets/src/test/groovy/com/example/fixtures/BucketFixtures.groovy
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package com.example.fixtures | ||
|
||
import software.amazon.awssdk.services.s3.model.Bucket | ||
import software.amazon.awssdk.services.s3.model.ListBucketsResponse | ||
|
||
class BucketFixtures { | ||
public static final String TEST_BUCKET_NAME = "demo-bucket" | ||
|
||
static listWithBucket(String bucketName = TEST_BUCKET_NAME) { | ||
def bucket = makeBucket(TEST_BUCKET_NAME) | ||
|
||
return ListBucketsResponse.builder() | ||
.buckets(bucket) | ||
.build() | ||
} | ||
|
||
static Bucket makeBucket(String bucketName) { | ||
Bucket.builder() | ||
.name(bucketName) | ||
.build() | ||
} | ||
} |