/**
  * Subscribe the annotated command handler to the given command bus.
  *
  * @param annotatedCommandHandler The annotated command handler that is to be subscribed to the
  *     command bus
  * @param commandBus The command bus that gets the handler's subscription
  * @return the Adapter created for the command handler target. Can be used to unsubscribe.
  */
 public static AnnotationCommandHandlerAdapter subscribe(
     Object annotatedCommandHandler, CommandBus commandBus) {
   AnnotationCommandHandlerAdapter adapter =
       new AnnotationCommandHandlerAdapter(annotatedCommandHandler, commandBus);
   adapter.subscribe();
   return adapter;
 }
 @Test
 public void testHandlerDispatching_WithReturnType() throws Throwable {
   Object actualReturnValue =
       testSubject.handle(GenericCommandMessage.asCommandMessage(1L), mockUnitOfWork);
   assertEquals(1L, actualReturnValue);
   assertEquals(0, mockTarget.voidHandlerInvoked);
   assertEquals(1, mockTarget.returningHandlerInvoked);
 }
 @Test
 public void testHandlerDispatching_ThrowingException() throws Throwable {
   try {
     testSubject.handle(GenericCommandMessage.asCommandMessage(new HashSet()), mockUnitOfWork);
     fail("Expected exception");
   } catch (Exception ex) {
     assertEquals(Exception.class, ex.getClass());
   }
 }
  @Test
  public void testSubscribe() {
    testSubject.subscribe();

    verify(mockBus).subscribe(Long.class, testSubject);
    verify(mockBus).subscribe(String.class, testSubject);
    verify(mockBus).subscribe(HashSet.class, testSubject);
    verify(mockBus).subscribe(ArrayList.class, testSubject);
    verifyNoMoreInteractions(mockBus);
  }
  /** This test should make sure the problem described in issue #91 does not occur anymore */
  @Test
  public void testCallbackCalled() {
    SimpleCommandBus scb = new SimpleCommandBus();
    AnnotationCommandHandlerAdapter.subscribe(this, scb);

    scb.dispatch(
        GenericCommandMessage.asCommandMessage("Hello"),
        new VoidCallback() {
          @Override
          protected void onSuccess() {
            // what I expected
          }

          @Override
          public void onFailure(Throwable cause) {
            cause.printStackTrace();
            fail("Did not expect a failure");
          }
        });
  }
 @Test(expected = NoHandlerForCommandException.class)
 public void testHandle_NoHandlerForCommand() throws Throwable {
   testSubject.handle(GenericCommandMessage.asCommandMessage(new LinkedList()), null);
 }