### Question:
TokenDataStore { public T read(String id) throws NotFoundException, ServerException { try { if (id == null) { throw new NotFoundException("Object not found"); } SyncResultHandler<Token> handler = new SyncResultHandler<Token>(); taskExecutor.execute(id, taskFactory.read(id, handler)); Token token = handler.getResults(); if (token == null) { throw new NotFoundException("Object not found with id: " + id); } return adapter.fromToken(token); } catch (NotFoundException e) { throw e; } catch (ServerException e) { throw e; } catch (DataLayerException e) { if (debug.warningEnabled()) { debug.warning("Unable to read token corresponding to id: " + id, e); } throw new ServerException("Could not read token from token data store: " + e.getMessage()); } } TokenDataStore(JavaBeanAdapter<T> adapter, TaskExecutor taskExecutor, TaskFactory taskFactory); @VisibleForTesting  TokenDataStore(JavaBeanAdapter<T> adapter, TaskExecutor taskExecutor, TaskFactory taskFactory, Debug debug); void create(T obj); T read(String id); void update(T obj); void delete(String id); Set<T> query(QueryFilter<String> query);  }### Answer:
@Test public void testRead() throws Exception { final Token token = new Token("123", TokenType.GENERIC); Object returned = new Object(); final Task task = mock(Task.class); when(adapter.fromToken(token)).thenReturn(returned); when(taskFactory.read(anyString(), any(ResultHandler.class))).thenAnswer(new Answer<Task>() { @Override public Task answer(InvocationOnMock invocation) throws Throwable { ((ResultHandler) invocation.getArguments()[1]).processResults(token); return task; } }); Object result = store.read("123"); verify(taskFactory).read(eq("123"), any(ResultHandler.class)); verify(taskExecutor).execute("123", task); assertThat(result).isSameAs(returned); }
@Test(expectedExceptions = NotFoundException.class) public void testReadNotExisting() throws Exception { final Token token = new Token("123", TokenType.GENERIC); Object returned = new Object(); final Task task = mock(Task.class); when(adapter.fromToken(token)).thenReturn(returned); when(taskFactory.read(anyString(), any(ResultHandler.class))).thenAnswer(new Answer<Task>() { @Override public Task answer(InvocationOnMock invocation) throws Throwable { ((ResultHandler) invocation.getArguments()[1]).processResults(null); return task; } }); store.read("123"); }
@Test(expectedExceptions = ServerException.class) public void testReadError() throws Exception { final Token token = new Token("123", TokenType.GENERIC); Object returned = new Object(); final Task task = mock(Task.class); when(adapter.fromToken(token)).thenReturn(returned); when(taskFactory.read(anyString(), any(ResultHandler.class))).thenAnswer(new Answer<Task>() { @Override public Task answer(InvocationOnMock invocation) throws Throwable { ((ResultHandler) invocation.getArguments()[1]).processError(new Exception()); return task; } }); store.read("123"); }
@Test(expectedExceptions = ServerException.class) public void testReadExecutorError() throws Exception { final Token token = new Token("123", TokenType.GENERIC); Object returned = new Object(); final Task task = mock(Task.class); when(adapter.fromToken(token)).thenReturn(returned); when(taskFactory.read(anyString(), any(ResultHandler.class))).thenAnswer(new Answer<Task>() { @Override public Task answer(InvocationOnMock invocation) throws Throwable { ((ResultHandler) invocation.getArguments()[1]).processResults(token); return task; } }); doThrow(DataLayerException.class).when(taskExecutor).execute(anyString(), any(Task.class)); store.read("123"); }