### Question:
TokenDataStore { public void create(T obj) throws ServerException { Token token = adapter.toToken(obj); SyncResultHandler<Token> handler = new SyncResultHandler<Token>(); try { taskExecutor.execute(token.getTokenId(), taskFactory.create(token, handler)); handler.getResults(); } catch (ServerException e) { throw e; } catch (DataLayerException e) { if (debug.warningEnabled()) { debug.warning("Unable to create token corresponding", e); } throw new ServerException("Could not create token in 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 testCreate() throws Exception { Token token = new Token("123", TokenType.GENERIC); final Task task = mock(Task.class); when(adapter.toToken(anyObject())).thenReturn(token); when(taskFactory.create(any(Token.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((ResultHandler) invocation.getArguments()[1]).processResults(new Object()); return task; } }); store.create(new Object()); verify(taskFactory).create(eq(token), any(ResultHandler.class)); verify(taskExecutor).execute("123", task); }
@Test(expectedExceptions = ServerException.class) public void testCreateError() throws Exception { Token token = new Token("123", TokenType.GENERIC); final Task task = mock(Task.class); when(adapter.toToken(anyObject())).thenReturn(token); when(taskFactory.create(any(Token.class), any(ResultHandler.class))).thenAnswer(new Answer<Object>() { @Override public Object answer(InvocationOnMock invocation) throws Throwable { ((ResultHandler) invocation.getArguments()[1]).processError(new Exception()); return task; } }); store.create(new Object()); }
@Test(expectedExceptions = ServerException.class) public void testCreateExecutorError() throws Exception { Token token = new Token("123", TokenType.GENERIC); final Task task = mock(Task.class); when(adapter.toToken(anyObject())).thenReturn(token); when(taskFactory.create(any(Token.class), any(ResultHandler.class))).thenReturn(task); doThrow(DataLayerException.class).when(taskExecutor).execute("123", task); store.create(new Object()); }