Skip to content

Commit 71817d8

Browse files
committed
feat: replace external dependencies with mocks in unit tests
- Remove DynamoDB Local dependency from unit tests - Replace integration tests with proper unit tests using Mockito - Add E2E test scenario for real AWS infrastructure validation - Improve test performance and reliability by eliminating external dependencies - Add mockito-core dependency to powertools-idempotency-dynamodb module
1 parent 08c9db8 commit 71817d8

File tree

5 files changed

+120
-388
lines changed

5 files changed

+120
-388
lines changed

powertools-e2e-tests/src/test/java/software/amazon/lambda/powertools/IdempotencyE2ET.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,4 +75,18 @@ public void test_ttlNotExpired_sameResult_ttlExpired_differentResult() throws In
7575
Assertions.assertThat(result2.getResult()).isEqualTo(result1.getResult());
7676
Assertions.assertThat(result3.getResult()).isNotEqualTo(result2.getResult());
7777
}
78+
79+
@Test
80+
public void test_idempotencyWithRealDynamoDBTable() {
81+
// GIVEN
82+
String uniqueEvent = "{\"message\":\"test-" + UUID.randomUUID().toString() + "\"}";
83+
84+
// WHEN
85+
InvocationResult result1 = invokeFunction(functionName, uniqueEvent);
86+
InvocationResult result2 = invokeFunction(functionName, uniqueEvent);
87+
88+
// THEN
89+
Assertions.assertThat(result1.getResult()).isNotNull();
90+
Assertions.assertThat(result2.getResult()).isEqualTo(result1.getResult());
91+
}
7892
}

powertools-idempotency/powertools-idempotency-dynamodb/pom.xml

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -60,21 +60,10 @@
6060

6161
<!-- Test dependencies -->
6262
<dependency>
63-
<groupId>com.amazonaws</groupId>
64-
<artifactId>DynamoDBLocal</artifactId>
65-
<!-- >2.2.0 is not compatible with Java 11 anymore -->
66-
<!-- https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/DynamoDBLocalHistory.html -->
67-
<version>2.2.0</version>
63+
<groupId>org.mockito</groupId>
64+
<artifactId>mockito-core</artifactId>
6865
<scope>test</scope>
6966
</dependency>
70-
<!--Needed when building locally on M1 Mac-->
71-
<dependency>
72-
<groupId>io.github.ganadist.sqlite4java</groupId>
73-
<artifactId>libsqlite4java-osx-aarch64</artifactId>
74-
<version>1.0.392</version>
75-
<scope>test</scope>
76-
<type>dylib</type>
77-
</dependency>
7867
</dependencies>
7968
<build>
8069
<plugins>

powertools-idempotency/powertools-idempotency-dynamodb/src/test/java/software/amazon/lambda/powertools/idempotency/persistence/dynamodb/DynamoDBConfig.java

Lines changed: 11 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -14,89 +14,20 @@
1414

1515
package software.amazon.lambda.powertools.idempotency.persistence.dynamodb;
1616

17-
import java.io.IOException;
18-
import java.net.ServerSocket;
19-
import java.net.URI;
17+
import org.junit.jupiter.api.BeforeEach;
18+
import org.mockito.Mock;
19+
import org.mockito.MockitoAnnotations;
2020

21-
import org.junit.jupiter.api.AfterAll;
22-
import org.junit.jupiter.api.BeforeAll;
23-
24-
import com.amazonaws.services.dynamodbv2.local.main.ServerRunner;
25-
import com.amazonaws.services.dynamodbv2.local.server.DynamoDBProxyServer;
26-
27-
import software.amazon.awssdk.auth.credentials.AwsBasicCredentials;
28-
import software.amazon.awssdk.auth.credentials.StaticCredentialsProvider;
29-
import software.amazon.awssdk.http.urlconnection.UrlConnectionHttpClient;
30-
import software.amazon.awssdk.regions.Region;
3121
import software.amazon.awssdk.services.dynamodb.DynamoDbClient;
32-
import software.amazon.awssdk.services.dynamodb.model.AttributeDefinition;
33-
import software.amazon.awssdk.services.dynamodb.model.BillingMode;
34-
import software.amazon.awssdk.services.dynamodb.model.CreateTableRequest;
35-
import software.amazon.awssdk.services.dynamodb.model.DescribeTableRequest;
36-
import software.amazon.awssdk.services.dynamodb.model.DescribeTableResponse;
37-
import software.amazon.awssdk.services.dynamodb.model.KeySchemaElement;
38-
import software.amazon.awssdk.services.dynamodb.model.KeyType;
39-
import software.amazon.awssdk.services.dynamodb.model.ScalarAttributeType;
4022

4123
public class DynamoDBConfig {
4224
protected static final String TABLE_NAME = "idempotency_table";
43-
protected static DynamoDBProxyServer dynamoProxy;
44-
protected static DynamoDbClient client;
45-
46-
@BeforeAll
47-
public static void setupDynamo() {
48-
int port = getFreePort();
49-
try {
50-
dynamoProxy = ServerRunner.createServerFromCommandLineArgs(new String[] {
51-
"-inMemory",
52-
"-port",
53-
Integer.toString(port)
54-
});
55-
dynamoProxy.start();
56-
} catch (Exception e) {
57-
throw new RuntimeException();
58-
}
59-
60-
client = DynamoDbClient.builder()
61-
.httpClient(UrlConnectionHttpClient.builder().build())
62-
.region(Region.EU_WEST_1)
63-
.endpointOverride(URI.create("http://localhost:" + port))
64-
.credentialsProvider(StaticCredentialsProvider.create(
65-
AwsBasicCredentials.create("FAKE", "FAKE")))
66-
.build();
67-
68-
client.createTable(CreateTableRequest.builder()
69-
.tableName(TABLE_NAME)
70-
.keySchema(KeySchemaElement.builder().keyType(KeyType.HASH).attributeName("id").build())
71-
.attributeDefinitions(
72-
AttributeDefinition.builder().attributeName("id").attributeType(ScalarAttributeType.S).build())
73-
.billingMode(BillingMode.PAY_PER_REQUEST)
74-
.build());
75-
76-
DescribeTableResponse response = client
77-
.describeTable(DescribeTableRequest.builder().tableName(TABLE_NAME).build());
78-
if (response == null) {
79-
throw new RuntimeException("Table was not created within expected time");
80-
}
81-
}
82-
83-
@AfterAll
84-
public static void teardownDynamo() {
85-
try {
86-
dynamoProxy.stop();
87-
} catch (Exception e) {
88-
throw new RuntimeException();
89-
}
90-
}
91-
92-
private static int getFreePort() {
93-
try {
94-
ServerSocket socket = new ServerSocket(0);
95-
int port = socket.getLocalPort();
96-
socket.close();
97-
return port;
98-
} catch (IOException ioe) {
99-
throw new RuntimeException(ioe);
100-
}
25+
26+
@Mock
27+
protected DynamoDbClient client;
28+
29+
@BeforeEach
30+
public void setupMocks() {
31+
MockitoAnnotations.openMocks(this);
10132
}
102-
}
33+
}

0 commit comments

Comments
 (0)