Announcement Announcement Module
Collapse
No announcement yet.
JNDI and Unit testing Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • JNDI and Unit testing

    What is the recommended way to test JNDI accessed objects within a basic unit test framework? I have a JMS topic and connection factory that I retrieve at runtime. I'd like to be able to configure ActiveMQ to run in-VM, along with a basic mocked JNDI with a bound topic and factory so that I can run the actual MQ within junit without a container. The only problem is that I can't figure out how to create a mock JNDI context within the applicationContext.xml and bind the two beans to it. Is there an easy way to do this?

    Unfortunately ActiveMQ did not include mock JNDI support in v1.0 so I can't use the info included here without using a development snapshot:

    http://activemq.codehaus.org/JNDI+Support

    mike

  • #2
    You can try using the SimpleNamingContext mock that is included with Spring, in the spring-mock.jar file.

    Comment


    • #3
      Do you have an example of such usage? I've looked at it once or twice today but it's not obvious to me how to use it.

      Comment


      • #4
        Also, why does this mock use constructor injection instead of the standard setter-based DI pattern that other Spring beans use?

        Comment


        • #5
          This does not work, I assume because <map> returns a non-Hashtable Map whereas the SimpleNamingContext constructor expects a Hashtable.

          Code:
          	<bean id="csaDocumentConnectionFactoryImpl" 
          		class="org.codehaus.activemq.ActiveMQConnectionFactory">
          		<property name="brokerURL"><value>vm&#58;//localhost</value></property>
          		<property name="useEmbeddedBroker"><value>true</value></property>
          	</bean>
          
          	<bean id="jndiContext" class="org.springframework.mock.jndi.SimpleNamingContext">
          		<constructor-arg index="0"><value>java&#58;comp/env/jms</value></constructor-arg>
              	<constructor-arg index="1">
                      <map>
                          <entry key="csaDocumentTCF"><ref bean="csaDocumentConnectionFactoryImpl"/></entry>
                      </map>
          		</constructor-arg>
          	    <constructor-arg index="2"><null/></constructor-arg>
          	</bean>

          Comment


          • #6
            Have a look at the javadocs of SimpleNamingContextBuilder: It shows a usage example. Essentially, you're supposed to activate a system-wide mock JNDI environment via SimpleNamingContextBuilder, rather than using SimpleNamingContext directly. The latter is just appropriate in very specific cases.

            Juergen

            Comment


            • #7
              The problem is that the usage shown uses Java code, not Spring context initialization. Instead I decided to use Sun's reference file system context and link to that in the appContext with a JndiTemplate. This way my test harnesses don't have to create their JNDI harness - it's provided to them just as under normal runtime conditions. I did have to write one-time Java code to create a .bindings file with the bound objects.

              Code:
              	<bean id="jndiTemplate" class="org.springframework.jndi.JndiTemplate">
              		<property name="environment">
              			<props>
              				<prop key="java.naming.factory.initial">com.sun.jndi.fscontext.RefFSContextFactory</prop>
              				<prop key="java.naming.provider.url">file&#58;./src/test</prop>
              			</props>
              		</property>
              	</bean>
              Originally posted by Juergen Hoeller
              Have a look at the javadocs of SimpleNamingContextBuilder: It shows a usage example. Essentially, you're supposed to activate a system-wide mock JNDI environment via SimpleNamingContextBuilder, rather than using SimpleNamingContext directly. The latter is just appropriate in very specific cases.

              Juergen

              Comment


              • #8
                How you have created your .binding file ?

                Thanks

                Comment

                Working...
                X