@Before
 public void setUp() {
   mockCommandBus = mock(CommandBus.class);
   mockApplicationContext = mock(ApplicationContext.class);
   testSubject = new AnnotationCommandHandlerBeanPostProcessor();
   testSubject.setCommandBus(mockCommandBus);
   testSubject.setApplicationContext(mockApplicationContext);
 }
  @Test
  public void testCommandBusIsAutowired() throws Exception {
    testSubject.setCommandBus(null);
    Map<String, CommandBus> map = new HashMap<String, CommandBus>();
    map.put("ignored", mockCommandBus);
    when(mockApplicationContext.getBeansOfType(CommandBus.class)).thenReturn(map);

    testSubject.afterPropertiesSet();

    verify(mockApplicationContext).getBeansOfType(CommandBus.class);
  }
  @Test
  public void testCommandBusIsNotAutowiredWhenProvided() throws Exception {

    testSubject.afterPropertiesSet();

    verify(mockApplicationContext, never()).getBeansOfType(CommandBus.class);
  }
  @SuppressWarnings({"unchecked"})
  @Test
  public void testEventHandlerAdapterIsInitializedAndDestroyedProperly() throws Exception {
    Object result1 =
        testSubject.postProcessBeforeInitialization(new AnnotatedCommandHandler(), "beanName");
    CommandHandler postProcessedBean =
        (CommandHandler) testSubject.postProcessAfterInitialization(result1, "beanName");

    verify(mockCommandBus).subscribe(eq(MyCommand.class.getName()), isA(CommandHandler.class));

    verify(mockCommandBus, never())
        .unsubscribe(eq(MyCommand.class.getName()), isA(CommandHandler.class));

    testSubject.postProcessBeforeDestruction(postProcessedBean, "beanName");

    verify(mockCommandBus).unsubscribe(eq(MyCommand.class.getName()), isA(CommandHandler.class));
  }
  @SuppressWarnings({"unchecked"})
  @Test
  public void testEventHandlerCallsRedirectToAdapter() throws Throwable {
    Object result1 =
        testSubject.postProcessBeforeInitialization(new AnnotatedCommandHandler(), "beanName");
    Object postProcessedBean = testSubject.postProcessAfterInitialization(result1, "beanName");

    assertTrue(Enhancer.isEnhanced(postProcessedBean.getClass()));
    assertTrue(postProcessedBean instanceof CommandHandler);
    assertTrue(postProcessedBean instanceof AnnotatedCommandHandler);

    CommandHandler<MyCommand> commandHandler = (CommandHandler<MyCommand>) postProcessedBean;
    AnnotatedCommandHandler annotatedCommandHandler = (AnnotatedCommandHandler) postProcessedBean;
    CommandMessage<MyCommand> myCommand = GenericCommandMessage.asCommandMessage(new MyCommand());
    commandHandler.handle(myCommand, null);

    assertEquals(1, annotatedCommandHandler.getInvocationCount());
  }