Skip to content

Commit 7c28076

Browse files
committed
Clarify how to mock the Lambda context in unit tests for Metrics utility.
1 parent 9038756 commit 7c28076

File tree

1 file changed

+18
-5
lines changed

1 file changed

+18
-5
lines changed

docs/core/metrics.md

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -558,10 +558,11 @@ If you would like to suppress metrics output during your unit tests, you can use
558558

559559
When unit testing your code, you can run assertions against the output generated by the `Metrics` Singleton. For the `EmfMetricsLogger`, you can assert the generated JSON blob following the [CloudWatch EMF specification](https://docs.aws.amazon.com/AmazonCloudWatch/latest/monitoring/CloudWatch_Embedded_Metric_Format.html) against your expected output.
560560

561-
Consider the following example where we redirect the standard output to a custom `PrintStream`. We use the Jackson library to parse the EMF output into a `JsonNode` and run assertions against that.
561+
Consider the following example where we redirect the standard output to a custom `PrintStream`. We use the Jackson library to parse the EMF output into a `JsonNode` and run assertions against that. Note that it is important to mock the Lambda `Context` with realistic values so that the metrics have valid metadata and dimensions during unit tests.
562562

563-
```java hl_lines="23 28 33 50-55"
563+
```java hl_lines="26-27 35 37-42 47 63-68"
564564
import static org.assertj.core.api.Assertions.assertThat;
565+
import static org.mockito.Mockito.when;
565566

566567
import java.io.ByteArrayOutputStream;
567568
import java.io.PrintStream;
@@ -571,24 +572,37 @@ import java.util.Map;
571572
import org.junit.jupiter.api.AfterEach;
572573
import org.junit.jupiter.api.BeforeEach;
573574
import org.junit.jupiter.api.Test;
575+
import org.junit.jupiter.api.extension.ExtendWith;
576+
import org.mockito.Mock;
577+
import org.mockito.junit.jupiter.MockitoExtension;
574578

575579
import com.amazonaws.services.lambda.runtime.Context;
576580
import com.amazonaws.services.lambda.runtime.RequestHandler;
577581
import com.fasterxml.jackson.databind.JsonNode;
578582
import com.fasterxml.jackson.databind.ObjectMapper;
579583

580584
import software.amazon.lambda.powertools.metrics.model.MetricUnit;
581-
import software.amazon.lambda.powertools.metrics.testutils.TestContext;
582585

586+
@ExtendWith(MockitoExtension.class)
583587
class MetricsTestExample {
584588

589+
@Mock
590+
Context lambdaContext;
591+
585592
private final PrintStream standardOut = System.out;
586593
private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
587594
private final ObjectMapper objectMapper = new ObjectMapper();
588595

589596
@BeforeEach
590597
void setUp() {
591598
System.setOut(new PrintStream(outputStreamCaptor));
599+
600+
// Mock a minimal realistic Lambda context. These values are used in metadata and dimensions by the Metrics utility.
601+
when(lambdaContext.getAwsRequestId()).thenReturn("test-request-id");
602+
when(lambdaContext.getFunctionName()).thenReturn("test-function");
603+
when(lambdaContext.getFunctionVersion()).thenReturn("test-version");
604+
when(lambdaContext.getInvokedFunctionArn()).thenReturn("test-arn");
605+
when(lambdaContext.getMemoryLimitInMB()).thenReturn(128);
592606
}
593607

594608
@AfterEach
@@ -600,11 +614,10 @@ class MetricsTestExample {
600614
void shouldCaptureMetricsFromAnnotatedHandler() throws Exception {
601615
// Given
602616
RequestHandler<Map<String, Object>, String> handler = new HandlerWithMetricsAnnotation();
603-
Context context = new TestContext();
604617
Map<String, Object> input = new HashMap<>();
605618

606619
// When
607-
handler.handleRequest(input, context);
620+
handler.handleRequest(input, lambdaContext);
608621

609622
// Then
610623
String emfOutput = outputStreamCaptor.toString().trim();

0 commit comments

Comments
 (0)