### Question:
GraphQLMetricHandler implements InvocationHandler { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { boolean success = false; Stopwatch stopwatch = Stopwatch.createStarted(); try { Object result = method.invoke(delegate, args); success = true; return result; } catch (InvocationTargetException e) { if (e.getCause() != null) { throw e.getCause(); } throw e; } finally { Tags tags = Tags .of("api", method.getDeclaringClass().getSimpleName()) .and("method", method.getName()) .and("result", success ? "success" : "failure") .and("authentication_group", AuthenticationGroup.of(authenticationSupplier.get()).name()); registry.timer("graphql_api", tags).record(stopwatch.elapsed()); } } GraphQLMetricHandler(Object delegate, MeterRegistry registry); @Override Object invoke(Object proxy, Method method, Object[] args);  }### Answer:
@Test public void success() throws Throwable { when(delegate.byKey(key)).thenReturn(Optional.of(domain)); Object result = underTest.invoke(null, method, new Object[] { key }); assertThat(result, is(Optional.of(domain))); verify(registry).timer("graphql_api", tags.and("result", "success").and("authentication_group", ANONYMOUS.name())); verify(timer).record(any(Duration.class)); }
@Test public void failure() throws Throwable { RuntimeException failed = new RuntimeException("failed"); when(delegate.byKey(key)).thenThrow(failed); try { underTest.invoke(null, method, new Object[] { key }); fail("Expected exception"); } catch (RuntimeException e) { assertThat(e, is(failed)); } verify(registry).timer("graphql_api", tags.and("result", "failure").and("authentication_group", ANONYMOUS.name())); verify(timer).record(any(Duration.class)); }
@Test public void authenticated() throws Throwable { when(authenticationSupplier.get()).thenReturn(new UsernamePasswordAuthenticationToken(null, null)); underTest.invoke(null, method, new Object[]{key}); verify(registry).timer("graphql_api", tags.and("result", "success").and("authentication_group", AUTHENTICATED.name())); }
@Test public void anonymous() throws Throwable { when(authenticationSupplier.get()).thenReturn(new AnonymousAuthenticationToken("key", "principal", singletonList(new SimpleGrantedAuthority("ANON")))); underTest.invoke(null, method, new Object[]{key}); verify(registry).timer("graphql_api", tags.and("result", "success").and("authentication_group", ANONYMOUS.name())); }