Example usage for org.springframework.context.annotation AnnotationConfigApplicationContext getBean

List of usage examples for org.springframework.context.annotation AnnotationConfigApplicationContext getBean

Introduction

In this page you can find the example usage for org.springframework.context.annotation AnnotationConfigApplicationContext getBean.

Prototype

@Override
    public <T> T getBean(String name, Class<T> requiredType) throws BeansException 

Source Link

Usage

From source file:org.springframework.yarn.support.statemachine.ListenerTests.java

@Test
public void testStateEvents() {
    AnnotationConfigApplicationContext ctx = new AnnotationConfigApplicationContext(Config.class);
    assertTrue(ctx.containsBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE));
    @SuppressWarnings("unchecked")
    EnumStateMachine<TestStates, TestEvents> machine = ctx
            .getBean(StateMachineSystemConstants.DEFAULT_ID_STATEMACHINE, EnumStateMachine.class);

    TestStateMachineListener listener = new TestStateMachineListener();
    machine.addStateListener(listener);/*from   ww  w.ja  va2  s .  c  o  m*/

    assertThat(machine, notNullValue());
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E1).setHeader("foo", "jee1").build());
    assertThat(listener.states.size(), is(1));
    assertThat(listener.states.get(0).from.getId(), is(TestStates.S1));
    assertThat(listener.states.get(0).to.getId(), is(TestStates.S2));
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E2).setHeader("foo", "jee2").build());
    assertThat(listener.states.size(), is(2));
    assertThat(listener.states.get(1).from.getId(), is(TestStates.S2));
    assertThat(listener.states.get(1).to.getId(), is(TestStates.S3));
    machine.sendEvent(MessageBuilder.withPayload(TestEvents.E4).setHeader("foo", "jee2").build());
    assertThat(listener.states.size(), is(2));

    ctx.close();
}