Code review comment: > the only reason for collecting the retrieved Gauge instances to a Set is to validate that none of them are null  Here, we are not checking for null; rather, we are examining whether there are duplicates among the five `Gauge`s to ensure that no metric has been registered multiple times.

Code under review:
void testWatermarkMetrics() throws Exception {
        final OneInputStreamTaskTestHarness<String, String> testHarness =
                new OneInputStreamTaskTestHarness<>(
                        OneInputStreamTask::new,
                        BasicTypeInfo.STRING_TYPE_INFO,
                        BasicTypeInfo.STRING_TYPE_INFO);

        OneInputStreamOperator<String, String> headOperator = new WatermarkMetricOperator();
        OperatorID headOperatorId = new OperatorID();

        OneInputStreamOperator<String, String> chainedOperator = new WatermarkMetricOperator();
        OperatorID chainedOperatorId = new OperatorID();

        testHarness
                .setupOperatorChain(headOperatorId, headOperator)
                .chain(
                        chainedOperatorId,
                        chainedOperator,
                        BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new SerializerConfigImpl()))
                .finish();

        InterceptingOperatorMetricGroup headOperatorMetricGroup =
                new InterceptingOperatorMetricGroup();
        InterceptingOperatorMetricGroup chainedOperatorMetricGroup =
                new InterceptingOperatorMetricGroup();
        InterceptingTaskMetricGroup taskMetricGroup =
                new InterceptingTaskMetricGroup() {
                    @Override
                    public InternalOperatorMetricGroup getOrAddOperator(
                            OperatorID id, String name) {
                        if (id.equals(headOperatorId)) {
                            return headOperatorMetricGroup;
                        } else if (id.equals(chainedOperatorId)) {
                            return chainedOperatorMetricGroup;
                        } else {
                            return super.getOrAddOperator(id, name);
                        }
                    }
                };

        StreamMockEnvironment env =
                new StreamMockEnvironment(
                        testHarness.jobConfig,
                        testHarness.taskConfig,
                        testHarness.memorySize,
                        new MockInputSplitProvider(),
                        testHarness.bufferSize,
                        new TestTaskStateManager()) {
                    @Override
                    public TaskMetricGroup getMetricGroup() {
                        return taskMetricGroup;
                    }
                };

        testHarness.invoke(env);
        testHarness.waitForTaskRunning();

        Gauge<Long> taskInputWatermarkGauge =
                (Gauge<Long>) taskMetricGroup.get(MetricNames.IO_CURRENT_INPUT_WATERMARK);
        Gauge<Long> headInputWatermarkGauge =
                (Gauge<Long>) headOperatorMetricGroup.get(MetricNames.IO_CURRENT_INPUT_WATERMARK);
        Gauge<Long> headOutputWatermarkGauge =
                (Gauge<Long>) headOperatorMetricGroup.get(MetricNames.IO_CURRENT_OUTPUT_WATERMARK);
        Gauge<Long> chainedInputWatermarkGauge =
                (Gauge<Long>)
                        chainedOperatorMetricGroup.get(MetricNames.IO_CURRENT_INPUT_WATERMARK);
        Gauge<Long> chainedOutputWatermarkGauge =
                (Gauge<Long>)
                        chainedOperatorMetricGroup.get(MetricNames.IO_CURRENT_OUTPUT_WATERMARK);

        assertThat(
                        new HashSet<>(
                                Arrays.asList(
                                        taskInputWatermarkGauge,
                                        headInputWatermarkGauge,
                                        headOutputWatermarkGauge,
                                        chainedInputWatermarkGauge,
                                        chainedOutputWatermarkGauge)))
                .as("A metric was registered multiple times.")
                .hasSize(5);

        assertThat(taskInputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);
        assertThat(headInputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);
        assertThat(headOutputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);
        assertThat(chainedInputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);
        assertThat(chainedOutputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);

        testHarness.processElement(new Watermark(1L));
        testHarness.waitForInputProcessing();
        assertThat(taskInputWatermarkGauge.getValue()).isOne();
        assertThat(headInputWatermarkGauge.getValue()).isOne();
        assertThat(headOutputWatermarkGauge.getValue()).isEqualTo(2L);
        assertThat(chainedInputWatermarkGauge.getValue()).isEqualTo(2L);
        assertThat(chainedOutputWatermarkGauge.getValue()).isEqualTo(4L);

        testHarness.processElement(new Watermark(2L));
        testHarness.waitForInputProcessing();
        assertThat(taskInputWatermarkGauge.getValue()).isEqualTo(2L);
        assertThat(headInputWatermarkGauge.getValue()).isEqualTo(2L);
        assertThat(headOutputWatermarkGauge.getValue()).isEqualTo(4L);
        assertThat(chainedInputWatermarkGauge.getValue()).isEqualTo(4L);
        assertThat(chainedOutputWatermarkGauge.getValue()).isEqualTo(8L);

        testHarness.endInput();
        testHarness.waitForTaskCompletion();
    }

Line commented on: assertThat(

Revised code:
void testWatermarkMetrics() throws Exception {
        final OneInputStreamTaskTestHarness<String, String> testHarness =
                new OneInputStreamTaskTestHarness<>(
                        OneInputStreamTask::new,
                        BasicTypeInfo.STRING_TYPE_INFO,
                        BasicTypeInfo.STRING_TYPE_INFO);

        OneInputStreamOperator<String, String> headOperator = new WatermarkMetricOperator();
        OperatorID headOperatorId = new OperatorID();

        OneInputStreamOperator<String, String> chainedOperator = new WatermarkMetricOperator();
        OperatorID chainedOperatorId = new OperatorID();

        testHarness
                .setupOperatorChain(headOperatorId, headOperator)
                .chain(
                        chainedOperatorId,
                        chainedOperator,
                        BasicTypeInfo.STRING_TYPE_INFO.createSerializer(new SerializerConfigImpl()))
                .finish();

        InterceptingOperatorMetricGroup headOperatorMetricGroup =
                new InterceptingOperatorMetricGroup();
        InterceptingOperatorMetricGroup chainedOperatorMetricGroup =
                new InterceptingOperatorMetricGroup();
        InterceptingTaskMetricGroup taskMetricGroup =
                new InterceptingTaskMetricGroup() {
                    @Override
                    public InternalOperatorMetricGroup getOrAddOperator(
                            OperatorID id, String name) {
                        if (id.equals(headOperatorId)) {
                            return headOperatorMetricGroup;
                        } else if (id.equals(chainedOperatorId)) {
                            return chainedOperatorMetricGroup;
                        } else {
                            return super.getOrAddOperator(id, name);
                        }
                    }
                };

        StreamMockEnvironment env =
                new StreamMockEnvironment(
                        testHarness.jobConfig,
                        testHarness.taskConfig,
                        testHarness.memorySize,
                        new MockInputSplitProvider(),
                        testHarness.bufferSize,
                        new TestTaskStateManager()) {
                    @Override
                    public TaskMetricGroup getMetricGroup() {
                        return taskMetricGroup;
                    }
                };

        testHarness.invoke(env);
        testHarness.waitForTaskRunning();

        Gauge<Long> taskInputWatermarkGauge =
                (Gauge<Long>) taskMetricGroup.get(MetricNames.IO_CURRENT_INPUT_WATERMARK);
        Gauge<Long> headInputWatermarkGauge =
                (Gauge<Long>) headOperatorMetricGroup.get(MetricNames.IO_CURRENT_INPUT_WATERMARK);
        Gauge<Long> headOutputWatermarkGauge =
                (Gauge<Long>) headOperatorMetricGroup.get(MetricNames.IO_CURRENT_OUTPUT_WATERMARK);
        Gauge<Long> chainedInputWatermarkGauge =
                (Gauge<Long>)
                        chainedOperatorMetricGroup.get(MetricNames.IO_CURRENT_INPUT_WATERMARK);
        Gauge<Long> chainedOutputWatermarkGauge =
                (Gauge<Long>)
                        chainedOperatorMetricGroup.get(MetricNames.IO_CURRENT_OUTPUT_WATERMARK);

        assertThat(
                        new HashSet<>(
                                Arrays.asList(
                                        taskInputWatermarkGauge,
                                        headInputWatermarkGauge,
                                        headOutputWatermarkGauge,
                                        chainedInputWatermarkGauge,
                                        chainedOutputWatermarkGauge)))
                .as("A metric was registered multiple times.")
                .hasSize(5);

        assertThat(taskInputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);
        assertThat(headInputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);
        assertThat(headOutputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);
        assertThat(chainedInputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);
        assertThat(chainedOutputWatermarkGauge.getValue()).isEqualTo(Long.MIN_VALUE);

        testHarness.processElement(new Watermark(1L));
        testHarness.waitForInputProcessing();
        assertThat(taskInputWatermarkGauge.getValue()).isOne();
        assertThat(headInputWatermarkGauge.getValue()).isOne();
        assertThat(headOutputWatermarkGauge.getValue()).isEqualTo(2L);
        assertThat(chainedInputWatermarkGauge.getValue()).isEqualTo(2L);
        assertThat(chainedOutputWatermarkGauge.getValue()).isEqualTo(4L);

        testHarness.processElement(new Watermark(2L));
        testHarness.waitForInputProcessing();
        assertThat(taskInputWatermarkGauge.getValue()).isEqualTo(2L);
        assertThat(headInputWatermarkGauge.getValue()).isEqualTo(2L);
        assertThat(headOutputWatermarkGauge.getValue()).isEqualTo(4L);
        assertThat(chainedInputWatermarkGauge.getValue()).isEqualTo(4L);
        assertThat(chainedOutputWatermarkGauge.getValue()).isEqualTo(8L);

        testHarness.endInput();
        testHarness.waitForTaskCompletion();
    }