Skip to content

Commit d327bff

Browse files
committed
Docs for testing your code.
1 parent 00412b6 commit d327bff

1 file changed

Lines changed: 76 additions & 0 deletions

File tree

docs/utilities/lambda_metadata.md

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,82 @@ The utility throws `LambdaMetadataException` when the metadata endpoint is unava
196196
}
197197
```
198198

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()`:
206+
207+
=== "MockedMetadataTest.java"
208+
209+
```java hl_lines="15-17"
210+
import software.amazon.lambda.powertools.metadata.LambdaMetadata;
211+
import software.amazon.lambda.powertools.metadata.LambdaMetadataClient;
212+
import org.mockito.MockedStatic;
213+
import org.junit.jupiter.api.Test;
214+
import static org.assertj.core.api.Assertions.assertThat;
215+
import static org.mockito.Mockito.*;
216+
217+
class MockedMetadataTest {
218+
219+
@Test
220+
void shouldUseMetadataInHandler() {
221+
LambdaMetadata mockMetadata = mock(LambdaMetadata.class);
222+
when(mockMetadata.getAvailabilityZoneId()).thenReturn("use1-az1");
223+
224+
try (MockedStatic<LambdaMetadataClient> mockedClient =
225+
mockStatic(LambdaMetadataClient.class)) {
226+
mockedClient.when(LambdaMetadataClient::get).thenReturn(mockMetadata);
227+
228+
App handler = new App();
229+
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:
240+
241+
=== "WireMockMetadataTest.java"
242+
243+
```java hl_lines="10-12"
244+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
245+
import static org.assertj.core.api.Assertions.assertThat;
246+
247+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
248+
import org.junitpioneer.jupiter.SetEnvironmentVariable;
249+
import org.junit.jupiter.api.Test;
250+
import software.amazon.lambda.powertools.metadata.LambdaMetadata;
251+
import software.amazon.lambda.powertools.metadata.internal.LambdaMetadataHttpClient;
252+
253+
@WireMockTest(httpPort = 8089)
254+
@SetEnvironmentVariable(key = "AWS_LAMBDA_METADATA_API", value = "localhost:8089")
255+
@SetEnvironmentVariable(key = "AWS_LAMBDA_METADATA_TOKEN", value = "test-token")
256+
class WireMockMetadataTest {
257+
258+
@Test
259+
void shouldFetchMetadataFromEndpoint() {
260+
stubFor(get(urlEqualTo("/2026-01-15/metadata/execution-environment"))
261+
.withHeader("Authorization", equalTo("Bearer test-token"))
262+
.willReturn(aResponse()
263+
.withStatus(200)
264+
.withHeader("Content-Type", "application/json")
265+
.withBody("{\"AvailabilityZoneID\": \"use1-az1\"}")));
266+
267+
LambdaMetadataHttpClient client = new LambdaMetadataHttpClient();
268+
LambdaMetadata metadata = client.fetchMetadata();
269+
270+
assertThat(metadata.getAvailabilityZoneId()).isEqualTo("use1-az1");
271+
}
272+
}
273+
```
274+
199275
## Using with other Powertools utilities
200276

201277
Lambda Metadata integrates seamlessly with other Powertools utilities to enrich your observability data with Availability Zone information.

0 commit comments

Comments
 (0)