ThreadTracker { @AroundInvoke public Object annotateThread(InvocationContext invCtx) throws Exception { String originName = Thread.currentThread().getName(); String beanName = invCtx.getTarget().getClass().getName(); String tracingName = beanName + "#" + invCtx.getMethod().getName() + " " + originName; try { Thread.currentThread().setName(tracingName); return invCtx.proceed(); } finally { System.out.println("After invocation: " + tracingName); Thread.currentThread().setName(originName); } }  @AroundInvoke Object annotateThread(InvocationContext invCtx);  }
@Test public void annotateThread() throws Exception { InvocationContext ic = mock(InvocationContext.class); Method method = ThreadTrackerTest.class.getMethod("annotateThread", (Class[])null); when(ic.getMethod()).thenReturn(method); when(ic.getTarget()).thenReturn(this); when(ic.proceed()).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ThreadTrackerTest.this.setThreadName(Thread.currentThread().getName()); return null; } }); this.cut.annotateThread(ic); assertTrue(threadName.startsWith(this.getClass().getName())); }