title: Spring Boot Datasource in unit tests
body: <p>I have a simple Spring Boot web app, that reads from a database and return a JSON response. I have the following test configuration:</p>
  
  <pre><code>@RunWith(SpringRunner.class)
  @SpringBootTest(classes=MyApplication.class, properties={"spring.config.name=myapp"})
  @AutoConfigureMockMvc
  public class ControllerTests {
      @Autowired
      private MockMvc mvc;
      @MockBean
      private ProductRepository productRepo;
      @MockBean
      private MonitorRepository monitorRepo;
  
      @Before
      public void setupMock() {
          Mockito.when(productRepo.findProducts(anyString(), anyString()))
          .thenReturn(Arrays.asList(dummyProduct()));     
      }
  
      @Test
      public void expectBadRequestWhenNoParamters() throws Exception {    
          mvc.perform(get("/products"))
                  .andExpect(status().is(400))
                  .andExpect(jsonPath("$.advice.status", is("ERROR")));
      }
  
      //other tests
  }
  </code></pre>
  
  <p>I have a DataSource bean that is configured in the main configuration of the application. When I run the tests Spring tries to load the context and fails, because the datasource is taken from JNDI. In general I want to avoid creating a datasource for this tests, because I have the repositories mocked.</p>
  
  <p>Is it possible to skip the creation of datasource when running the unit tests?</p>
  
  <p>In memory database for testing is not an option, because my database creation script has a specific structure and cannot be easily executed from classpath:schema.sql</p>
  
  <p><strong>Edit</strong>
  The datasource is defined in <code>MyApplication.class</code></p>
  
  <pre><code>    @Bean
      DataSource dataSource(DatabaseProeprties databaseProps) throws NamingException {
         DataSource dataSource = null;
         JndiTemplate jndi = new JndiTemplate();
         setJndiEnvironment(databaseProps, jndi);
         try {
             dataSource = jndi.lookup(databaseProps.getName(), DataSource.class);
         } catch (NamingException e) {
             logger.error("Exception loading JNDI datasource", e);
             throw e;
         }
         return dataSource;
     }
  </code></pre>