### Question:
StateMachine { @SuppressWarnings("unchecked") public Transition handleEvent(Transition last, E event) { final Object current = last.getState(); final Context context = last.getContext(); final String stateMachineId = context.stateMachineId(); Transition next; try { final HandleEvent<E> handleEvent = defByState.get(current.getClass()); if (handleEvent != null) { next = handleEvent.handle(last, event); } else { LOG.error("unhandled event {}", event); next = unhandled(last, event); } } catch (Exception e) { LOG.error("SM [" + stateMachineId + "], Error handling event [" + event + "]", e); next = onError(event, context, e); } next = next.setPrevious(last); invokeOnTransitionListeners(last, next, event); return next; }  @SuppressWarnings("unchecked") Transition handleEvent(Transition last, E event);  }### Answer:
@Test public void shouldRequestAndAcceptSuccessfully() { final Transition requested = stateMachine.handleEvent(initialTransition, new RechargeEvent()); assertThat(requested).isNotNull(); assertThat(requested.getState().getClass()).isEqualTo(RECHARGE_REQUESTED_STATE.getClass()); final Optional<PinRechargedContext> pinRechagedContext = requested.getContext().mostRecentOf(PinRechargedContext.class); assertThat(pinRechagedContext.isPresent()).isEqualTo(true); assertThat(onTransitionStack.size()).isEqualTo(0); final Transition accepted = stateMachine.handleEvent(requested, new RechargeAcceptedEvent()); assertThat(accepted).isNotNull(); assertThat(accepted.getState().getClass()).isEqualTo(RECHARGE_ACCEPTED_FINAL_STATE.getClass()); assertThat(onTransitionStack.size()).isEqualTo(1); assertThat(onTransitionStack.get(0).getState().getClass()).isEqualTo(RechargeAcceptedFinalState.class); assertThat(onTransitionStack.get(0).previous().get().getState().getClass()) .isEqualTo(RechargeRequestedState.class); }