title: Spring Hibernate H2 Junit testing - how to load schema on start
body: <p>I am trying to develop tests for my application (I found out about the tests very late...) and I am stuck and the basic configuration. I have googled through many examples and none of them satisfied me and frankly left me a bit confused.</p>
  
  <p>What I am trying to achieve is to load an import.sql on start of the test (which is a dump file from existing MySQL schema) and load it into H2 database.</p>
  
  <p><strong>Here is the hibernate config file:</strong></p>
  
  <pre><code>@Configuration
  @EnableTransactionManagement
  @ComponentScan({ "kamienica.feature" })
  public class HibernateTestConfiguration {
  
      @Autowired
      ApplicationContext applicationContext;
  
      @Bean
      public LocalSessionFactoryBean sessionFactory() {
          LocalSessionFactoryBean sessionFactory = new LocalSessionFactoryBean();
          sessionFactory.setDataSource(dataSource());
          sessionFactory.setPackagesToScan(new String[] { "kamienica" });
          sessionFactory.setHibernateProperties(hibernateProperties());
          return sessionFactory;
      }
  
      @Bean(name = "dataSource")
      public DataSource dataSource() {
          DriverManagerDataSource dataSource = new DriverManagerDataSource();
          dataSource.setDriverClassName("org.h2.Driver");
          dataSource.setUrl("jdbc:h2:mem:test;DB_CLOSE_DELAY=-1;"
                  + "INIT=CREATE SCHEMA IF NOT EXISTS kamienica;DB_CLOSE_ON_EXIT=FALSE");
          dataSource.setUsername("sa");
          dataSource.setPassword("");
  
          return dataSource;
      }
  
      private Properties hibernateProperties() {
          Properties properties = new Properties();
          properties.put("hibernate.dialect", "org.hibernate.dialect.H2Dialect");
          properties.put("hibernate.hbm2ddl.auto", "update");
  // this is where I tried to load script the first time:
  // properties.put("hibernate.hbm2ddl.import_files", "kamienica.sql");
          return properties;
      }
  
      @Bean
      @Autowired
      public HibernateTransactionManager transactionManager(SessionFactory s) {
          HibernateTransactionManager txManager = new HibernateTransactionManager();
          txManager.setSessionFactory(s);
          return txManager;
      }
  }
  </code></pre>
  
  <p>Everytime I start a test I get a message that:</p>
  
  <blockquote>
    <p>org.hibernate.tool.hbm2ddl.DatabaseMetadata getTableMetadata INFO:
    HHH000262: Table not found: apartment</p>
  </blockquote>
  
  <p>And I get empty/null values when trying to retrieve anything</p>
  
  <p><strong>I have tried to load sql file in the hibernate config (via hibernate properties) as well as in superclass which all my test classes are planned to extend:</strong></p>
  
  <pre><code>@RunWith(SpringJUnit4ClassRunner.class)
  @ContextConfiguration(classes = { HibernateTestConfiguration.class })
  public class BaseTest {
  
      private EmbeddedDatabase db;
  
      @Autowired
      private SessionFactory sessionFactory;
  
      //second attempt to load sql file
      @Before
      public void setUp() throws Exception {
          db = new EmbeddedDatabaseBuilder().addScript("import.sql").build();
      }
  
      @After
      public void tearDown() throws Exception {
          SessionHolder sessionHolder = (SessionHolder) TransactionSynchronizationManager.unbindResource(sessionFactory);
          SessionFactoryUtils.closeSession(sessionHolder.getSession());
      }
  
  }
  </code></pre>
  
  <p>How can I load sql file and prepare the H2 database to perform the tests?</p>