CdiBootstrap { public <T> T getBean(Class<T> cls) { logger.info("Generate bean {}, from {}, cause native injection doesn't work.", cls, Constants.BeanManager.BEANMANAGER_ALT); 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)); }  BeanManager getBeanManager(); T getBean(Class<T> cls);  }
@Test public void testGetBean() throws NamingException { System.out.println("getBean"); Bean b = mock(Bean.class); when(b.getBeanClass()).thenReturn(Result.class); Set<Bean<?>> beans = new HashSet<>(); beans.add(b); CreationalContext context = mock(CreationalContext.class); BeanManager bm = mock(BeanManager.class); when(bm.getBeans(eq(Result.class), any(Annotation.class))).thenReturn(beans); when(bm.createCreationalContext(eq(b))).thenReturn(context); when(bm.getReference(eq(b), eq(Result.class), eq(context))).thenReturn(new Result()); when(cdiBootstrap.getBeanManager()).thenReturn(bm); Object result = cdiBootstrap.getBean(Result.class); assertThat(result).isInstanceOf(Result.class); }