forked from aws-samples/serverless-test-samples
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAppWithMockTest.java
52 lines (45 loc) · 2 KB
/
AppWithMockTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
/*
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: MIT-0
*/
package com.example;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyRequestEvent;
import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent;
import com.amazonaws.services.lambda.runtime.tests.annotations.Event;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.http.HttpStatus;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.extension.ExtendWith;
import org.junit.jupiter.params.ParameterizedTest;
import org.mockito.junit.jupiter.MockitoExtension;
import software.amazon.awssdk.services.s3.S3Client;
import software.amazon.awssdk.services.s3.model.Bucket;
import software.amazon.awssdk.services.s3.model.ListBucketsResponse;
import java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
@ExtendWith(MockitoExtension.class)
public class AppWithMockTest {
private static S3Client s3Client;
@BeforeEach
public void setup() {
s3Client = mock(S3Client.class);
Bucket bucket = mock(Bucket.class);
when(bucket.name()).thenReturn("foo");
lenient().when(s3Client.listBuckets()).thenReturn(ListBucketsResponse.builder().buckets(bucket).build());
}
@ParameterizedTest
@Event(value = "events/apigw_req_s3_buckets_get.json", type = APIGatewayProxyRequestEvent.class)
public void successfulGetResponse(APIGatewayProxyRequestEvent event) throws IOException {
App app = new App(s3Client);
APIGatewayProxyResponseEvent response = app.handleRequest(event, null);
Assertions.assertNotNull(response);
assertEquals(HttpStatus.SC_OK, response.getStatusCode().intValue());
assertEquals("application/json", response.getHeaders().get("Content-Type"));
List content = new ObjectMapper().readValue(response.getBody(), List.class);
assertNotNull(content);
assertTrue(content.size() > 0);
}
}