JmxMetricsAcquirer { @PostConstruct public void init() { for (Map.Entry<String, MetricDescription> metric : metricDescriptions.entrySet()) { Metric m = new Gauge<Long>() { @Override public Long getValue() { MetricDescription desc = metric.getValue(); Object o; try { o = server.getAttribute(desc.getObjectName(), desc.getName()); return metric.getValue().getConverter().apply(o); } catch (AttributeNotFoundException | InstanceNotFoundException | MBeanException | ReflectionException e) { return null; } } }; metricRegistry.register(MetricRegistry.name(JmxMetricsAcquirer.class, metric.getKey(), metric.getKey()), m); } } @Autowired  JmxMetricsAcquirer(MetricRegistry metricRegistry, @Value("${datadog.report.period:1}") int period,
            @Value("${datadog.report.timeunit:SECONDS}") TimeUnit timeUnit,
            @Value("${jmx.metrics.excluded:}") String excluded); @PostConstruct void init();  }
@Test public void test() { MetricRegistry metricRegistry = mock(MetricRegistry.class); JmxMetricsAcquirer acquirer = new JmxMetricsAcquirer(metricRegistry, 1, TimeUnit.SECONDS, "jvm.heap_memory_max,jvm.non_heap_memory_max"); acquirer.init(); verify(metricRegistry, times(0)).register(eq("jvm.heap_memory_max"), any()); verify(metricRegistry, times(0)).register(eq("jvm.non_heap_memory_max"), any()); }