TimedInterceptor implements MethodInterceptor { @Override public Object invoke(MethodInvocation invocation) throws Throwable { String timerName = invocation.getMethod().getAnnotation(Timed.class).value(); if (timerName.isEmpty()) { timerName = MetricRegistry.name(invocation.getThis().getClass().getSuperclass(), invocation.getMethod().getName()); } Timer.Context timerContext = metricsServiceProvider .get() .getMetricRegistry() .timer(timerName) .time(); try { return invocation.proceed(); } finally { timerContext.stop(); } } @Inject  TimedInterceptor(Provider<MetricsService> metricsServiceProvider); @Override Object invoke(MethodInvocation invocation);  }
@Test public void testStopCalledNotClosed() throws Throwable { timedInterceptor.invoke(invocation); Mockito.verify(timerContext, Mockito.times(1)).stop(); Mockito.verify(timerContext, Mockito.times(0)).close(); }