ExecutionContextRunnable implements Runnable { @Override public void run() { ExecutionContext context = new ExecutionContext(); try { this.componentManager.<ExecutionContextManager>getInstance(ExecutionContextManager.class) .initialize(context); } catch (Exception e) { throw new RuntimeException( String.format("Failed to initialize Runnable [%s] execution context", this.runnable), e); } try { this.runnable.run(); } finally { try { this.componentManager.<Execution>getInstance(Execution.class).removeContext(); } catch (ComponentLookupException e) { throw new RuntimeException(String.format( "Failed to cleanup Execution Context after Runnable [%s] execution", this.runnable), e); } } } ExecutionContextRunnable(Runnable runnable, ComponentManager componentManager); @Override void run();  }
@Test void initializedExecutionContext() throws InterruptedException, ComponentLookupException, ExecutionContextException { ComponentManager componentMangerMock = Mockito.mock(ComponentManager.class); ExecutionContextManager executionContextManagerMock = Mockito.mock(ExecutionContextManager.class); Execution executionMock = Mockito.mock(Execution.class); Mockito.when(componentMangerMock.getInstance(ExecutionContextManager.class)).thenReturn( executionContextManagerMock); Mockito.when(componentMangerMock.getInstance(Execution.class)).thenReturn(executionMock); Runnable runnableMock = Mockito.mock(Runnable.class); Thread thread = new Thread(new ExecutionContextRunnable(runnableMock, componentMangerMock)); thread.start(); thread.join(10000); verify(executionContextManagerMock).initialize(any(ExecutionContext.class)); verify(executionMock).removeContext(); verify(runnableMock).run(); }