Search in sources :

Example 1 with TestApplicationListener

use of org.springframework.context.testfixture.beans.TestApplicationListener in project spring-framework by spring-projects.

the class EventPublicationInterceptorTests method testExpectedBehavior.

@Test
public void testExpectedBehavior() {
    TestBean target = new TestBean();
    final TestApplicationListener listener = new TestApplicationListener();
    class TestContext extends StaticApplicationContext {

        @Override
        protected void onRefresh() throws BeansException {
            addApplicationListener(listener);
        }
    }
    StaticApplicationContext ctx = new TestContext();
    MutablePropertyValues pvs = new MutablePropertyValues();
    pvs.add("applicationEventClass", TestEvent.class.getName());
    // should automatically receive applicationEventPublisher reference
    ctx.registerSingleton("publisher", EventPublicationInterceptor.class, pvs);
    ctx.registerSingleton("otherListener", FactoryBeanTestListener.class);
    ctx.refresh();
    EventPublicationInterceptor interceptor = (EventPublicationInterceptor) ctx.getBean("publisher");
    ProxyFactory factory = new ProxyFactory(target);
    factory.addAdvice(0, interceptor);
    ITestBean testBean = (ITestBean) factory.getProxy();
    // invoke any method on the advised proxy to see if the interceptor has been invoked
    testBean.getAge();
    // two events: ContextRefreshedEvent and TestEvent
    assertThat(listener.getEventCount() == 2).as("Interceptor must have published 2 events").isTrue();
    TestApplicationListener otherListener = (TestApplicationListener) ctx.getBean("&otherListener");
    assertThat(otherListener.getEventCount() == 2).as("Interceptor must have published 2 events").isTrue();
}
Also used : ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestApplicationListener(org.springframework.context.testfixture.beans.TestApplicationListener) ITestBean(org.springframework.beans.testfixture.beans.ITestBean) TestBean(org.springframework.beans.testfixture.beans.TestBean) StaticApplicationContext(org.springframework.context.support.StaticApplicationContext) TestEvent(org.springframework.context.event.test.TestEvent) ProxyFactory(org.springframework.aop.framework.ProxyFactory) MutablePropertyValues(org.springframework.beans.MutablePropertyValues) Test(org.junit.jupiter.api.Test)

Aggregations

Test (org.junit.jupiter.api.Test)1 ProxyFactory (org.springframework.aop.framework.ProxyFactory)1 MutablePropertyValues (org.springframework.beans.MutablePropertyValues)1 ITestBean (org.springframework.beans.testfixture.beans.ITestBean)1 TestBean (org.springframework.beans.testfixture.beans.TestBean)1 TestEvent (org.springframework.context.event.test.TestEvent)1 StaticApplicationContext (org.springframework.context.support.StaticApplicationContext)1 TestApplicationListener (org.springframework.context.testfixture.beans.TestApplicationListener)1