diff --git a/docs/core/metrics.md b/docs/core/metrics.md
index 31079a76e..61d4c38f0 100644
--- a/docs/core/metrics.md
+++ b/docs/core/metrics.md
@@ -140,7 +140,7 @@ For most use-cases, we recommend using Environment variables and only overwrite
Type: AWS::Serverless::Function
Properties:
...
- Runtime: java8
+ Runtime: java11
Environment:
Variables:
POWERTOOLS_SERVICE_NAME: payment
@@ -558,9 +558,24 @@ If you would like to suppress metrics output during your unit tests, you can use
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.
+Make sure to set a test metrics namespace and service name to run assertions against metrics. For example, by setting the following environment variables in your tests:
+
+```xml
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+
+
+ TestService
+ TestNamespace
+
+
+
+```
+
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.
-```java hl_lines="23 28 33 50-55"
+```java hl_lines="35 40 56-72"
import static org.assertj.core.api.Assertions.assertThat;
import java.io.ByteArrayOutputStream;
@@ -571,6 +586,9 @@ import java.util.Map;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtendWith;
+import org.mockito.Mock;
+import org.mockito.junit.jupiter.MockitoExtension;
import com.amazonaws.services.lambda.runtime.Context;
import com.amazonaws.services.lambda.runtime.RequestHandler;
@@ -578,21 +596,25 @@ import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import software.amazon.lambda.powertools.metrics.model.MetricUnit;
-import software.amazon.lambda.powertools.metrics.testutils.TestContext;
+@ExtendWith(MockitoExtension.class)
class MetricsTestExample {
+ @Mock
+ Context lambdaContext;
+
private final PrintStream standardOut = System.out;
- private final ByteArrayOutputStream outputStreamCaptor = new ByteArrayOutputStream();
+ private ByteArrayOutputStream outputStreamCaptor;
private final ObjectMapper objectMapper = new ObjectMapper();
@BeforeEach
void setUp() {
+ outputStreamCaptor = new ByteArrayOutputStream();
System.setOut(new PrintStream(outputStreamCaptor));
}
@AfterEach
- void tearDown() {
+ void tearDown() throws Exception {
System.setOut(standardOut);
}
@@ -600,27 +622,37 @@ class MetricsTestExample {
void shouldCaptureMetricsFromAnnotatedHandler() throws Exception {
// Given
RequestHandler