1
votes

I have successfully tested some DAO outside a weblogic server by looking up the datasource information through jndi. I have search for a similar option with websphere and have to yet come accross a solution that does not involve hardcoding the username and password in some location within the application or something similar. Right now my jndi settings look like this inside spring:

<bean id="applicationServerEnviromentProperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean">
<property name="properties"><props>
<prop key="java.naming.factory.initial">com.ibm.websphere.naming.WsnInitialContextFactory</prop>
<prop key="java.naming.provider.url">iiop://localhost:2809</prop>
</props>
</property>
</bean>

<bean id="dataSource" class="org.springframework.jndi.JndiObjectFactoryBean">
        <property name="jndiName"><value>PeopleAppDS</value></property>
        <property name="jndiEnvironment"><ref local="applicationServerEnviromentProperties"/></property>
    </bean>

I have Tested the jndi connection and it is working when the application is loaded on to websphere. I would like to be able to test the daos inside eclipse for instance before the app is loaded. Any help would be much appreciated.

Here are the details for the test case.

-------BaseTestCase.java--------------------

import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations= {"file:data-access-config.xml"})
public class BaseTestCase {

}

-----PersonDaoTest.java----------------

import static org.junit.Assert.assertNotNull;

import java.util.List;<br>

import org.junit.Test;<br>
import org.springframework.beans.factory.annotation.Autowired;

import ....dao.PersonDao;<br>
import ....domain.Person;<br>

public class PersonDaoTest extends BaseTestCase {

    @Autowired
    private PersonDao personDao;


    @Test
    public void findByName() {
        List<Person>  people = personDao.listByName("j%", false, "userId");
        assertNotNull(people);
    }
}
1

1 Answers

0
votes

The right way to do it is to have a JNDI data source with a DriverManagerDataSource default. If you run in the container, Spring will use the named data source; if the lookup fails, it'll use the non-JNDI data source.