Meaningless Drivel is fun!*
The moose likes Spring and the fly likes JNDI Datasource Injecting using Spring for JUnit throwing exception Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "JNDI Datasource Injecting using Spring for JUnit throwing exception" Watch "JNDI Datasource Injecting using Spring for JUnit throwing exception" New topic
Forums: Testing Spring
Author

JNDI Datasource Injecting using Spring for JUnit throwing exception

santoshini patra
Greenhorn

Joined: Aug 22, 2013
Posts: 2
Hi...

I have a scenario where I need to have the datasources to be injected to the JNDI which are being used later during JUnit test case execution. I have the spring related xml configuration in place, and I am able to bing the datasource also but while opening connection to the Database I am getting the exception as "java.sql.SQLException: Io exception: JNDI Package failurejavax.naming.NotContextException: Not an instance of DirContext" while running the test case, I searched number of forums and tried different options but not able to find the root cause. The datasource is available in Weblogic server. Any help is greatly appreciated. Below are the xml config details.

spring-test.xml
<bean id="testDs" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="oracle.jdbc.driver.OracleDriver"/>
<property name="url" value="URL"/>
<property name="username" value="INVADJ_APP"/>
<property name="password" value="****"/>
</bean>

JNDI Declaration in test-operation.xml
<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
<property name="jndiName" value="IRDTORACLEConnectionPool"/>
</bean>

Below is how I am binding the datasource
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-test.xml");
DataSource ds=(DataSource)context.getBean("testDs");
SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
builder.bind("IRDTORACLEConnectionPool",ds);
Please let me know if any other details are reqruied.

Thank you so much....!!!
Bill Gorder
Bartender

Joined: Mar 07, 2010
Posts: 1632
    
    7

Hello and welcome to the CodeRanch!

JNDI support is provided by the container (in your case WebLogic). It is not available in your unit tests. You could provide a different test context that does not use JNDI for running your test cases. If this is an essential part of what you are testing you can have a look at the org.springframework.mock.jndi package for useful classes to achieve this.

Typically though unit test are just testing a small unit of work. Bringing in the entire production xml configuration file is more than you need. It is typically simpler and more efficient to create a test-context.xml file containing only the beans required to complete the test. Often times your repository calls are mocked in this case anyway and all of your current problems are really a non-issue.

Thanks
Bill


[How To Ask Questions][Read before you PM me]
santoshini patra
Greenhorn

Joined: Aug 22, 2013
Posts: 2
Hi Bill...

Thank you for the reply and appreciate it.

Actually my productin code uses the JNDI name of the connectoin datasource to connect to database, so while running my JUnit test case I should be having the datasource of the same name in my JUnit environment, for which I am using Spring to inject the JNDI. I used org.springframework.mock.jndi.SimpleNamingContextBuilder for the JNDI binding but I am getting exception "java.sql.SQLException: Io exception: JNDI Package failurejavax.naming.NotContextException: Not an instance of DirContext" while doing "connectionObj.getConnection()" I am not able to find out what the real issue is.

Any help is greatly appreciated.

Thanks So much... !!!
 
 
subject: JNDI Datasource Injecting using Spring for JUnit throwing exception
 
Similar Threads
exceptions in spring configuration file.
JPA's context injection with Hibernate as engine
Injection Problem
A Simple Test Framework using JUnit and Spring (Part 1)
JPA-hibernate-spring config