CdiResolver implements IDataServiceResolver { @Override public <T> T resolveDataService(Class<T> clazz) throws DataServiceException { Set<Bean<?>> beans = beanManager.getBeans(clazz, new DataserviceLiteral()); for (Bean<?> b : beans) { final CreationalContext context = beanManager.createCreationalContext(b); return clazz.cast(beanManager.getReference(b, b.getBeanClass(), context)); } throw new DataServiceException(clazz.getName()); }  @Override T resolveDataService(Class<T> clazz); @Override Scope getScope(Class clazz);  }
@Test(expected = DataServiceException.class) public void testResolveDataServiceFail() throws DataServiceException { System.out.println("resolveDataServiceFail"); when(beanManager.getBeans(CDIBeanSession.class)).thenReturn(new HashSet<Bean<?>>()); cdiResolver.resolveDataService(CDIBeanSession.class); }
@Test public void testResolveDataService() throws DataServiceException { System.out.println("resolveDataService"); CDIBeanSession expResult = new CDIBeanSession(); Set<Bean<?>> set = new HashSet<>(); Bean b = mock(Bean.class); set.add(b); when(beanManager.getBeans(any(Type.class), any(DataService.class))).thenReturn(set); when(beanManager.createCreationalContext(any(Bean.class))).thenReturn(null); when(beanManager.getReference(any(Bean.class), any(Class.class), any(CreationalContext.class))).thenReturn(expResult); Object result = cdiResolver.resolveDataService(CDIBeanSession.class); assertThat(result).isInstanceOf(CDIBeanSession.class); }