Announcement Announcement Module
Collapse
No announcement yet.
How to register a Mocked ApplicationListener Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • How to register a Mocked ApplicationListener

    Hi,

    I'm trying to unit test a service that sends ApplicationEvent.
    So I want to mock (with easyMock) an ApplicationListener.

    I test the MockFactory proposed by Ronen [1] but the well created instance of my ApplicationListener is not registered in the ApplicationContext.

    For instance, my MockApplicationListenerFactory

    Code:
    public class MockApplicationListenerFactory implements FactoryBean, InitializingBean {
    	
    	private ApplicationListener applicationListener;
    	
    	private Class<ApplicationListener> type;
    	
    	public void afterPropertiesSet() {
    		this.applicationListener = EasyMock.createMock(type);
    	}
    		
    	public void setType(final Class<ApplicationListener> type) {
    		this.type = type;
    	}
    
    	@Override
    	public Object getObject() throws Exception {
    		return this.applicationListener;
    	}
    
    	@Override
    	public Class<ApplicationListener> getObjectType() {
    		return ApplicationListener.class;
    	}
    
    	@Override
    	public boolean isSingleton() {
    		return true;
    	}
    
    }
    And the test context

    Code:
    <bean id="reportListener" class="my.test.MockApplicationListenerFactory">
    		<property name="type" value="org.springframework.context.ApplicationListener" />
    	</bean>
    Am I missing something for register the listener in applicationContext ?

    [1] http://javadevelopmentforthemasses.b...ing-tests.html

  • #2
    After some debugging time on spring-2.5.6, I came to the conclusion of a lack of the ApplicationContext which is not able to register an ApplicationListener via bean factory.

    I plan to open an issue if someone is able to confirm that point.

    However, it is of course possible that I miss something, so please let me know

    Comment


    • #3
      It is strange that object produced by factory bean is not registered as application listeners, but it is possible to add it programmatically:

      Code:
              ApplicationEventMulticaster applicationEventMulticaster = (ApplicationEventMulticaster) applicationContext.getBean("applicationEventMulticaster");
              applicationEventMulticaster.addApplicationListener((ApplicationListener) applicationContext.getBean("reportListener"));
      Code:
          <bean id="applicationEventMulticaster" class="org.springframework.context.event.SimpleApplicationEventMulticaster" />
      
          <bean id="reportListener" class="com.example.MocksFactory">
              <property name="type" value="org.springframework.context.ApplicationListener" />
          </bean>

      Comment


      • #4
        Thanks a lot Andrei,

        I register programmatically my Mock Listener and I'm now able to verify the different event send by my service. It's a cool stuff that Easy Mock. It disappointing me a lot, but I'm on the way to love it.

        Comment

        Working...
        X