title: Replacing DataSource in Mockito
body: <p>I have code which performs query on Jboss server. It has JNDI based datasource and looks like this:</p>
  
  <pre><code>    public class JNDIBasedDao {
  
          DataSource dataSource;
  
          public JNDIBasedDao(){
  
              try {
                  InitialContext ic = new InitialContext();
                  dataSource = (DataSource) ic.lookup("java://bla-bla-bla");
              } catch (NamingException e) {
              e.printStackTrace();
              }
  
  
          }
  
  public class Manager {
  
      public Manager(){}
  
      private JNDIBasedDao dao = new JNDIBasedDao();
  
      public void runOperation(){
          this.dao.executeInsert();
      }
  </code></pre>
  
  <p>On my laptop I have no Jboss and no ability to connect to this server and want to run Unit testing on HSQLDB.</p>
  
  <p>I want to create BasicDataSource from apache commons based on HSQLDB and inject this object into JNDIBasedDao.</p>
  
  <pre><code>@Mock
  BasicDataSource dataSource = new BasicDataSource();
  
  @Mock 
  JNDIBasedDao dao = new JNDIBasedDao();
  
  @InjectMocks
  Manager manager = new Manager();
  @Before
      public void initMocks(){
          dataSource.setDriverClassName("org.hsqldb.jdbcDriver");
          dataSource.setUrl("jdbc:hsqldb:mem:dannyTest");
          dataSource.setUsername("sa");
          dataSource.setPassword("");
          dataSource.setInitialSize(5);
          dataSource.setMaxActive(10);
          dataSource.setPoolPreparedStatements(true);
          dataSource.setMaxOpenPreparedStatements(10);
  
          MockitoAnnotations.initMocks(this);
  
      }
  
      @Test
      public void testRunOperartion() {
          manager.runOperartion();
      }
  </code></pre>
  
  <p>but I'm still getting JNDI error. Can it be done? Please help.</p>