MessageToClientManager { Object _getDataService(T session, Class cls) throws DataServiceException { String dataServiceClassName = cls.getName(); DataService dataServiceAnno = (DataService) cls.getAnnotation(DataService.class); IDataServiceResolver resolver = getResolver(dataServiceAnno.resolver()); Scope scope = resolver.getScope(cls); Object dataService = null; Map<String, Object> sessionBeans = getSessionBeans(session); logger.debug("{} : scope : {}", dataServiceClassName, scope); if (scope.equals(Scope.SESSION)) { dataService = sessionBeans.get(dataServiceClassName); logger.debug("{} : scope : session is in session : {}", dataServiceClassName, (dataService != null)); } if (dataService == null) { dataService = resolver.resolveDataService(cls); if (scope.equals(Scope.SESSION)) { logger.debug("Store {} scope session in session", dataServiceClassName); sessionBeans.put(dataServiceClassName, dataService); } } return dataService; }  abstract Map<String, Object> getSessionBeans(T session); MessageToClient createMessageToClient(MessageFromClient message, T session);  }
@Test public void test_GetDataService() throws Exception { System.out.println("_getDataService"); Class cls = ClassAsDataService.class; IDataServiceResolver resolver = mock(IDataServiceResolver.class); Session session = mock(Session.class); Map<String, Object> sessions = new HashMap<>(); ClassAsDataService ds1 = new ClassAsDataService(); ClassAsDataService ds2 = new ClassAsDataService(); ClassAsDataService ds3 = new ClassAsDataService(); when(resolver.getScope(any(Class.class))).thenReturn(Scope.MANAGED).thenReturn(Scope.SESSION); when(resolver.resolveDataService(cls)).thenReturn(ds1).thenReturn(ds2).thenReturn(ds3); doReturn(sessions).when(instance).getSessionBeans(session); doReturn(resolver).when(instance).getResolver("TEST"); Object result = instance._getDataService(session, cls); assertThat(result).isEqualTo(ds1); assertThat(sessions).doesNotContainKey(cls.getName()); result = instance._getDataService(session, cls); assertThat(result).isEqualTo(ds2); assertThat(sessions).containsKey(cls.getName()); result = instance._getDataService(session, cls); assertThat(result).isEqualTo(ds2); assertThat(sessions).containsKey(cls.getName()); result = instance._getDataService(null, cls); assertThat(result).isEqualTo(ds3); }