@Test
  public void testAddRemoveListener() throws Exception {
    long startTime = System.currentTimeMillis();
    StatisticsManager mockStatisticsManager =
        mock(StatisticsManager.class, testName.getMethodName() + "$StatisticsManager");
    when(mockStatisticsManager.getName()).thenReturn("mockStatisticsManager");
    when(mockStatisticsManager.getStartTime()).thenReturn(startTime);
    when(mockStatisticsManager.getStatListModCount()).thenReturn(0);
    when(mockStatisticsManager.getStatsList()).thenReturn(new ArrayList<>());

    StatisticsSampler mockStatisticsSampler =
        mock(StatisticsSampler.class, testName.getMethodName() + "$StatisticsSampler");
    when(mockStatisticsSampler.getStatisticsModCount()).thenReturn(0);
    when(mockStatisticsSampler.getStatistics()).thenReturn(new Statistics[] {});

    // need a real SampleCollector for this test or the monitor can't get the handler
    SampleCollector sampleCollector = new SampleCollector(mockStatisticsSampler);
    sampleCollector.initialize(mockStatArchiveHandlerConfig, NanoTimer.getTime(),
        new MainWithChildrenRollingFileHandler());

    List<StatisticsNotification> notifications = new ArrayList<>();
    StatisticsListener listener = notifications::add;

    ValueMonitor monitor = new ValueMonitor();
    Number value = 43;
    Type type = Type.VALUE_CHANGED;
    long timeStamp = System.currentTimeMillis();
    StatisticsNotification notification = createStatisticsNotification(timeStamp, type, value);
    monitor.notifyListeners(notification);
    assertThat(notifications.isEmpty()).isTrue();

    monitor.addListener(listener);
    monitor.notifyListeners(notification);
    assertThat(notifications.size()).isEqualTo(1);
    notification = notifications.remove(0);
    assertThat(notification).isNotNull();
    assertThat(notification.getTimeStamp()).isEqualTo(timeStamp);
    assertThat(notification.getType()).isEqualTo(type);
    StatisticId statId = mock(StatisticId.class);
    assertThat(notification.getValue(statId)).isEqualTo(value);

    monitor.removeListener(listener);
    monitor.notifyListeners(notification);
    assertThat(notifications.isEmpty()).isTrue();
  }
Integration test for the SampleCollector class.

@since GemFire 7.0
geode-core/src/integrationTest/java/org/apache/geode/internal/statistics/ValueMonitorIntegrationTest.java