diff --git a/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java b/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java index 3301dcd96a..882cc6785f 100644 --- a/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/core/MetricsTest.java @@ -16,14 +16,16 @@ public class MetricsTest { @Test public void whenMarkMeter_thenCorrectRates() throws InterruptedException { Meter meter = new Meter(); - long initCount = meter.getCount(); + assertThat(initCount, equalTo(0L)); meter.mark(); + assertThat(meter.getCount(), equalTo(1L)); meter.mark(20); + assertThat(meter.getCount(), equalTo(21L)); // not use assert for these rate values because they change every time when this test is run @@ -40,6 +42,7 @@ public class MetricsTest { @Test public void whenInitRatioGauge_thenCorrectRatio() { Gauge ratioGauge = new AttendanceRatioGauge(15, 20); + assertThat(ratioGauge.getValue(), equalTo(0.75)); } @@ -48,6 +51,7 @@ public class MetricsTest { Gauge> activeUsersGauge = new ActiveUsersGauge(15, TimeUnit.MINUTES); List expected = new ArrayList(); expected.add(12L); + assertThat(activeUsersGauge.getValue(), equalTo(expected)); } @@ -55,36 +59,42 @@ public class MetricsTest { public void whenUseDerivativeGauge_thenCorrectGaugeFromBase() { Gauge> activeUsersGauge = new ActiveUsersGauge(15, TimeUnit.MINUTES); Gauge activeUserCountGauge = new ActiveUserCountGauge(activeUsersGauge); + assertThat(activeUserCountGauge.getValue(), equalTo(1)); } @Test public void whenIncDecCounter_thenCorrectCount() { Counter counter = new Counter(); - long initCount = counter.getCount(); + assertThat(initCount, equalTo(0L)); counter.inc(); + assertThat(counter.getCount(), equalTo(1L)); counter.inc(11); + assertThat(counter.getCount(), equalTo(12L)); counter.dec(); + assertThat(counter.getCount(), equalTo(11L)); counter.dec(6); + assertThat(counter.getCount(), equalTo(5L)); } @Test public void whenUpdateHistogram_thenCorrectDistributionData() { Histogram histogram = new Histogram(new UniformReservoir()); - histogram.update(5); long count1 = histogram.getCount(); + assertThat(count1, equalTo(1L)); + Snapshot snapshot1 = histogram.getSnapshot(); assertThat(snapshot1.getValues().length, equalTo(1)); assertThat(snapshot1.getValues()[0], equalTo(5L)); @@ -101,8 +111,11 @@ public class MetricsTest { histogram.update(20); long count2 = histogram.getCount(); + assertThat(count2, equalTo(2L)); + Snapshot snapshot2 = histogram.getSnapshot(); + assertThat(snapshot2.getValues().length, equalTo(2)); assertThat(snapshot2.getValues()[0], equalTo(5L)); assertThat(snapshot2.getValues()[1], equalTo(20L)); @@ -121,11 +134,11 @@ public class MetricsTest { @Test public void whenUseTimer_thenCorrectTimerContexts() throws InterruptedException { Timer timer = new Timer(); - Timer.Context context1 = timer.time(); TimeUnit.SECONDS.sleep(5); long elapsed1 = context1.stop(); + assertEquals(5000000000L, elapsed1, 1000000); assertThat(timer.getCount(), equalTo(1L)); assertEquals(0.2, timer.getMeanRate(), 0.1); @@ -133,6 +146,7 @@ public class MetricsTest { Timer.Context context2 = timer.time(); TimeUnit.SECONDS.sleep(2); context2.close(); + assertThat(timer.getCount(), equalTo(2L)); assertEquals(0.3, timer.getMeanRate(), 0.1); } diff --git a/metrics/src/test/java/com/baeldung/metrics/healthchecks/HealthCheckTest.java b/metrics/src/test/java/com/baeldung/metrics/healthchecks/HealthCheckTest.java index 99a3970761..b1b2cc2ff9 100644 --- a/metrics/src/test/java/com/baeldung/metrics/healthchecks/HealthCheckTest.java +++ b/metrics/src/test/java/com/baeldung/metrics/healthchecks/HealthCheckTest.java @@ -23,10 +23,13 @@ public class HealthCheckTest { assertThat(healthCheckRegistry.getNames().size(), equalTo(2)); Map results = healthCheckRegistry.runHealthChecks(); + assertFalse(results.isEmpty()); + results.forEach((k, v) -> assertTrue(v.isHealthy())); healthCheckRegistry.unregister("uc"); + assertThat(healthCheckRegistry.getNames().size(), equalTo(1)); } } \ No newline at end of file