use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.
the class ApplicationContextEventTests method initMethodPublishesEvent.
@Test
public void initMethodPublishesEvent() {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
context.registerBeanDefinition("initMethod", new RootBeanDefinition(EventPublishingInitMethod.class));
context.refresh();
context.publishEvent(new MyEvent(this));
BeanThatListens listener = context.getBean(BeanThatListens.class);
assertThat(listener.getEventCount()).isEqualTo(3);
context.close();
}
use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.
the class ApplicationContextEventTests method innerBeanAsListener.
@Test
public void innerBeanAsListener() {
StaticApplicationContext context = new StaticApplicationContext();
RootBeanDefinition listenerDef = new RootBeanDefinition(TestBean.class);
listenerDef.getPropertyValues().add("friends", new RootBeanDefinition(BeanThatListens.class));
context.registerBeanDefinition("listener", listenerDef);
context.refresh();
context.publishEvent(new MyEvent(this));
context.publishEvent(new MyEvent(this));
TestBean listener = context.getBean(TestBean.class);
assertThat(((BeanThatListens) listener.getFriends().iterator().next()).getEventCount()).isEqualTo(3);
context.close();
}
use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.
the class ApplicationContextEventTests method initMethodPublishesAsyncEvent.
@Test
public void initMethodPublishesAsyncEvent() {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
context.registerBeanDefinition("initMethod", new RootBeanDefinition(AsyncEventPublishingInitMethod.class));
context.refresh();
context.publishEvent(new MyEvent(this));
BeanThatListens listener = context.getBean(BeanThatListens.class);
assertThat(listener.getEventCount()).isEqualTo(3);
context.close();
}
use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.
the class ApplicationContextEventTests method beanPostProcessorPublishesEvents.
@Test
public void beanPostProcessorPublishesEvents() {
GenericApplicationContext context = new GenericApplicationContext();
context.registerBeanDefinition("listener", new RootBeanDefinition(BeanThatListens.class));
context.registerBeanDefinition("messageSource", new RootBeanDefinition(StaticMessageSource.class));
context.registerBeanDefinition("postProcessor", new RootBeanDefinition(EventPublishingBeanPostProcessor.class));
context.refresh();
context.publishEvent(new MyEvent(this));
BeanThatListens listener = context.getBean(BeanThatListens.class);
assertThat(listener.getEventCount()).isEqualTo(4);
context.close();
}
use of org.springframework.context.testfixture.beans.BeanThatListens in project spring-framework by spring-projects.
the class AbstractApplicationContextTests method beanAutomaticallyHearsEvents.
@Test
public void beanAutomaticallyHearsEvents() throws Exception {
// String[] listenerNames = ((ListableBeanFactory) applicationContext).getBeanDefinitionNames(ApplicationListener.class);
// assertTrue("listeners include beanThatListens", Arrays.asList(listenerNames).contains("beanThatListens"));
BeanThatListens b = (BeanThatListens) applicationContext.getBean("beanThatListens");
b.zero();
assertThat(b.getEventCount() == 0).as("0 events before publication").isTrue();
this.applicationContext.publishEvent(new MyEvent(this));
assertThat(b.getEventCount() == 1).as("1 events after publication, not " + b.getEventCount()).isTrue();
}
Aggregations