CdiInstanceResolver { @SuppressWarnings("unchecked") public <T> T getInstanceOf(final Class<T> beanClass, final BeanManager beanManager) { try { final Bean<?> bean = beanManager.resolve(beanManager.getBeans(beanClass)); final CreationalContext<?> context = beanManager.createCreationalContext(bean); return (T) beanManager.getReference( bean, beanClass, context); } catch (final Exception e) { throw new UnresolvableCdiInstanceException(format("Failed to find class %s in CDI", beanClass.getName()), e); } }  @SuppressWarnings("unchecked") T getInstanceOf(final Class<T> beanClass, final BeanManager beanManager);  }
@SuppressWarnings("unchecked") @Test public void shouldResolveTheInstanceOfACdiBean() throws Exception { final BeanManager beanManager = mock(BeanManager.class); final Bean bean = mock(Bean.class); final Set<Bean<?>> beans = newHashSet(bean); final CreationalContext<?> context = mock(CreationalContext.class); final DummyCdiBean systemCommandScanner = new DummyCdiBean(); final Class<DummyCdiBean> beanClass = DummyCdiBean.class; when(beanManager.getBeans(beanClass)).thenReturn(beans); when(beanManager.resolve(beans)).thenReturn(bean); when(beanManager.createCreationalContext(bean)).thenReturn(context); when(beanManager.getReference( bean, beanClass, context)).thenReturn(systemCommandScanner); assertThat(cdiInstanceResolver.getInstanceOf(beanClass, beanManager), is(systemCommandScanner)); }