Announcement Announcement Module
Collapse
No announcement yet.
Plugging in null implementations Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Plugging in null implementations

    I'm working on making my Struts and Spring application more testable. As part of that, I've seperated the Spring configuration dealing with each layer of my application into separate files. So, one for the web stuff (Struts action classes, etc), one for a service layer and one for a DAO layer. When I'm testing the struts action classes (with StrutsTestCase), I want to configure Spring to give me an action class with the dependencies null. I need to keep Spring in the mix (although I'd rather not) because of the way I'm integrating Spring and Struts. See http://www-128.ibm.com/developerwork...ary/j-sr2.html Option #3 for a description of my approach. I know I could put the null in place if I kept a seperate instance of my web config file and used a <null> attribute. However, I don't want to do that. I'd rather replace my regular service config file with a testing one. Maybe I can illustrate:

    Here's a snippet from ApplicationContext-web.xml:

    Code:
    
        <beans>
         
        	<bean name="/viewEvents" class="org.appname.web.action.ViewAllEvents">
        		<property name="eventManager">
        			<ref bean="eventManager" />
        		</property>
        	</bean>	
        </beans>

    Here's the ApplicationContext-service-test.xml:

    Code:
    
        <beans>
        	<bean id="eventManager"
        		class="">
        		
        	</bean>
         
        </beans>

    Any ideas?

    Thanks,
    Ben

  • #2
    We do something like this by bean switching using properties from a properties file loaded using a PropertyPlaceholderConfigurer. Using your example, this would look like:

    Code:
    <beans>
        <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
            <property name="locations">
                <list>
                    <value>classpath:your.properties</value>
                </list>
            </property>
        </bean>
    
        <bean id="eventManager"
    class="org.springframework.beans.factory.config.BeanReferenceFactoryBean">
            <property name="targetBeanName">
                <value>eventManager${event.manager}</value>
            </property>		
        </bean>
    
        <bean id="eventManagerReal" class="EventManager" lazy="true"/>
    
        <bean id="eventManagerNull"  class="" lazy="true"/>
    </beans>
    Now in your property file you can set:

    Code:
    event.manager=Null
    for the null event manager or:

    Code:
    event.manager=Real
    for the real one. You can configure the property placeholder configurer so that you can override properties using system properties, so you could switch beans using a -Devent.manager=Null on the command line.

    Comment


    • #3
      I'm particularly interested in how you made the eventManagerNull as null.
      Code:
      <bean id="eventManagerNull"  class="" lazy="true"/>
      I'll have to try that out.

      Thanks,
      Ben

      Comment


      • #4
        Sorry - I just copied the null implementation from your original example - I've never tried doing this. You might have to go one level higher and do the switching on your "/viewEvents" bean instead. Alternately you could create a factory bean (i.e. implement the FactoryBean interface) that returns null.

        This is starting to get complicated, so I'd go back to looking at what you are actually trying to do. Rather than setting your event manager to null for the tests (how were you stopping this generating null pointer exceptions?), I would look at wiring in a mock implementation instead. We use EasyMock which is surprisingly easy to use for this sort of thing once you get your head around how it works.

        Comment

        Working...
        X