Results 1 to 4 of 4

Thread: How to register a Mocked ApplicationListener

  1. #1

    Default 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. #2

    Default

    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

  3. #3
    Join Date
    Jun 2007
    Location
    Minsk, Belarus
    Posts
    224

    Default

    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>

  4. #4

    Default

    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.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •