You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/utilities/lambda_metadata.md
+76Lines changed: 76 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -196,6 +196,82 @@ The utility throws `LambdaMetadataException` when the metadata endpoint is unava
196
196
}
197
197
```
198
198
199
+
## Testing your code
200
+
201
+
When running outside a Lambda execution environment (e.g., in unit tests), the `AWS_LAMBDA_METADATA_API` and `AWS_LAMBDA_METADATA_TOKEN` environment variables are not available. Calling `LambdaMetadataClient.get()` in this context throws a `LambdaMetadataException`.
202
+
203
+
### Mocking LambdaMetadataClient
204
+
205
+
For tests where you need to control the metadata values, use Mockito's `mockStatic` to mock `LambdaMetadataClient.get()`:
String result = handler.handleRequest(null, null);
230
+
231
+
assertThat(result).contains("use1-az1");
232
+
}
233
+
}
234
+
}
235
+
```
236
+
237
+
### Using WireMock
238
+
239
+
For integration tests, you can use [WireMock](https://wiremock.org/){target="_blank"} to mock the metadata HTTP endpoint. Set `AWS_LAMBDA_METADATA_API` and `AWS_LAMBDA_METADATA_TOKEN` environment variables using [junit-pioneer](https://junit-pioneer.org/docs/environment-variables/){target="_blank"}, and stub the endpoint response:
0 commit comments