Mocking the JNDI lookup

Sometimes, we need to mock the <jee:jndi-lookup>/JNDI lookup with a mock value in the out-of-container tests. The org.springframework.mock.jndi package contains an implementation of the JNDI SPI, which you can use to set up a simple JNDI environment for test suites or standalone applications. In the following example, we'll define <jee:jndi-lookup> for the DataSource resource in applicationContext and mock out the lookup from the test. The following are the steps to mock up a JNDI call:

  1. Create an applicationContext.xml file in the com.packt.jndi package, with the following details:
    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:jee="http://www.springframework.org/schema/jee"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
      xsi:schemaLocation="
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/jee
    http://www.springframework.org/schema/jee/spring-jee-4.1.xsd
    ">
    
      
    <jee:jndi-lookup id="common-Datasource" 
                jndi-name="java:comp/env/Datasource"
        resource-ref="true" cache="true" 
                lookup-on-startup="false"
        proxy-interface="javax.sql.DataSource" />
      
    
    </beans>
  2. When we run a JUnit test, the container is not accessible; hence, we need to mock out the <jee:jndi-lookup> from our JUnit test. We'll create an ApplicationContextInitializer instance to initialize the application context and bind a mock DataSource object with the original DataSource name. The following is the test code:
    @RunWith(SpringJUnit4ClassRunner.class)
    @ContextConfiguration(locations = "classpath:com/packt/jndi/applicationContext.xml", 
        initializers = 
           DataSourceTest.MockJeeLookUpInitializer.class)
    public class DataSourceTest {
    
      @Autowired
      ApplicationContext context;
    
      @Test
      public void jndiResource() throws Exception {
        assertNotNull(context.getBean("common-Datasource"));
      }
    
      public static class MockJeeLookUpInitializer implements
            ApplicationContextInitializer
              <ConfigurableApplicationContext> {
    
      @Override
      public void initialize(
        ConfigurableApplicationContext applicationContext) {
          DataSource mockDataSource = (javax.sql.DataSource)
              Mockito.mock(javax.sql.DataSource.class);
          SimpleNamingContextBuilder builder = new 
              SimpleNamingContextBuilder();
          builder.bind("java:comp/env/Datasource", 
              mockDataSource);
           try {
            builder.activate();
          } catch (IllegalStateException | 
                NamingException e) {
            e.printStackTrace();
          }
        }
      }
    }

    A SimpleNamingContextBuilder object is created and then a mock DataSource object is bound to the name java:comp/env/Datasource; finally, the builder is activated in the ApplicationContextInitializer interface.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.80.45