I have a scenario where I need to have the datasources to be injected to the JNDI which are being used later during JUnittest 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.
Below is how I am binding the datasource
ApplicationContext context=new ClassPathXmlApplicationContext("classpath:spring-test.xml");
SimpleNamingContextBuilder builder = SimpleNamingContextBuilder.emptyActivatedContextBuilder();
Please let me know if any other details are reqruied.
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.
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.