CdiBeanResolver { public <T> T getBean(Class<T> cls) { BeanManager bm = getBeanManager(); Set<Bean<?>> beans = bm.getBeans(cls, DEFAULT_AT); Bean<?> b = beans.iterator().next(); final CreationalContext context = bm.createCreationalContext(b); return cls.cast(bm.getReference(b, b.getBeanClass(), context)); }  T getBean(Class<T> cls);  }
@Test public void testGetBean() throws NamingException { System.out.println("getBean"); CdiBeanResolver instance = spy(CdiBeanResolver.class); CdiBeanResolver.raz(); InitialContext ic = mock(InitialContext.class); BeanManager bm = mock(BeanManager.class); Set<Bean<?>> beans = new HashSet<>(); Bean<?> b = mock(Bean.class); beans.add(b); CreationalContext context = mock(CreationalContext.class); doReturn(ic).when(instance).getInitialContext(); when(ic.lookup(eq(Constants.BeanManager.BEANMANAGER_JEE))).thenReturn(bm); when(bm.getBeans(any(Class.class), any(Annotation.class))).thenReturn(beans); when(bm.createCreationalContext(eq(b))).thenReturn(context); when(bm.getReference(eq(b), any(Class.class), eq(context))).thenReturn(this); Object result = instance.getBean(this.getClass()); assertThat(result).isInstanceOf(this.getClass()); }