title: How to generate H2 embedded database during Mockito unit test?
body: <p>I'm currently working on a springboot API where it has been decided that me and my coworkers will be using an H2 embedded database during development.</p>
  
  <p>The database is generating itself fine upon starting the application, but I can't make it work during unit test : they all fail saying <code>org.h2.jdbc.JdbcSQLException: Table '[...]' not found</code>.</p>
  
  <p>I basically did not write most of this code, please let me know if you need more :</p>
  
  <p><code>application.properties</code> file where the H2 database is initialized :</p>
  
  <pre><code>datasources.member-request.url=jdbc:h2:mem:db;IGNORECASE=TRUE;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE;INIT=CREATE SCHEMA IF NOT EXISTS DMDEOWNER\\;SET SCHEMA DMDEOWNER\\;runscript from 'classpath:h2_init_script.sql'
  datasources.member-request.username=sa
  datasources.member-request.password=
  datasources.member-request.driver-class-name=org.h2.Driver
  datasources.member-request.initialize=true
  </code></pre>
  
  <p>As you can see above, we execute a SQL script at the initialisation of H2 called <code>h2_init_script.sql</code>, in this file we have some SQL statement like this (I'll show you the T_THEME table for example) </p>
  
  <pre class="lang-sql prettyprint-override"><code>/* CREATE statements */
  CREATE TABLE DMDEOWNER.T_THEME (
      F_NUM_THEME NUMBER(14) NOT NULL,
      F_REF_THEME VARCHAR2(30) NOT NULL,
      F_LIB_THEME VARCHAR2(50) NOT NULL,
      F_POS_THEME NUMBER(4) NOT NULL,
      F_BOOL_ACTIF NUMBER(1) NOT NULL,
      F_UTILISATEUR_MAJ VARCHAR2(40) NOT NULL,
      F_DT_CREATION DATE NOT NULL,
      F_DT_MAJ DATE,
      PRIMARY KEY (F_NUM_THEME)
  );
  
  /* INSERT statements */
  /* ... */
  </code></pre>
  
  <p>Here is my test class (and its parent) :</p>
  
  <pre class="lang-java prettyprint-override"><code>public class ThemeRepositoryTest extends AbstractRepositoryTest {
  
    private final static int NB_ACTIVE_ENTITIES = 2;
    private final static int NB_INACTIVE_ENTITIES = 1;
  
    @Autowired
    private ThemeRepository repository;
  
    @Autowired
    private ThemeMapper mapper;
  
  
    // -------------------------------------------------------------------------
    // CREATE
    // -------------------------------------------------------------------------
    @Before
    public void init() {
      for (int i = INTEGER_ZERO; i &lt; NB_ACTIVE_ENTITIES; i++) {
        insertTheme(true);
      }
      for (int i = INTEGER_ZERO; i &lt; NB_INACTIVE_ENTITIES; i++) {
        insertTheme(false);
      }
      entityManager.flush();
    }
  
    // -------------------------------------------------------------------------
    // READ
    // -------------------------------------------------------------------------
    @Test
    public void findAll() {
      List&lt;ThemeEntity&gt; list = repository.findAll();
      assertEquals(NB_ACTIVE_ENTITIES + NB_INACTIVE_ENTITIES, list.size());
    }
  }
  
  @RunWith(SpringRunner.class)
  @DataJpaTest
  public abstract class AbstractRepositoryTest {
  
    @Autowired
    protected TestEntityManager entityManager;
  
    protected ThemeEntity insertTheme(boolean active) {
      ThemeEntity entity =  newThemeEntity(3L, "NAME", active, "USER");
      return entityManager.merge(entity);
    }
  }
  </code></pre>
  
  <p>Here is where my error happens, in the <code>init()</code> function when I call <code>insertTheme()</code> :</p>
  
  <pre><code>findAll(io.lacipav.repository.ThemeRepositoryTest)  Time elapsed: 0.227 s  &lt;&lt;&lt; ERROR!
  javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not prepare statement
      at io.lacipav.repository.ThemeRepositoryTest.init(ThemeRepositoryTest.java:38)
  Caused by: org.hibernate.exception.SQLGrammarException: could not prepare statement
      at io.lacipav.repository.ThemeRepositoryTest.init(ThemeRepositoryTest.java:38)
  Caused by: org.h2.jdbc.JdbcSQLException: 
  Table "T_THEME" not found; SQL statement:
  select themeentit0_.f_num_theme as f_num_th1_11_0_, themeentit0_.f_bool_actif as f_bool_a2_11_0_, themeentit0_.f_dt_creation as f_dt_cre3_11_0_, themeentit0_.f_dt_maj as f_dt_maj4_11_0_, themeentit0_.f_lib_theme as f_lib_th5_11_0_, themeentit0_.f_pos_theme as f_pos_th6_11_0_, themeentit0_.f_ref_theme as f_ref_th7_11_0_, themeentit0_.f_utilisateur_maj as f_utilis8_11_0_ from t_theme themeentit0_ where themeentit0_.f_num_theme=? [42102-197]
      at io.lacipav.repository.ThemeRepositoryTest.init(ThemeRepositoryTest.java:38)
  </code></pre>
  
  <p>Since the error is telling me that it did not find my table, I'm guessing it is because my database isn't running when I'm building my <code>.war</code> file, if so how can I initialize it quicker ?</p>
  
  <p>EDIT : 
  As asked for, here's the Entity class for T_THEME :</p>
  
  <pre class="lang-java prettyprint-override"><code>@Entity
  @Getter
  @Setter
  @Table(name = "T_THEME")
  public class ThemeEntity {
  
    /** Primary key of the table. */
    @Id
    @NotNull
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "theme_generator")
    @SequenceGenerator(name = "theme_generator", sequenceName = "SEQ_NUM_THEME", allocationSize = 1)
    @Column(name = "f_num_theme")
    private Long idTechnique;
  
    /** Theme functional identifier */
    @NotNull
    @Column(name = "f_ref_theme")
    private String reference;
  
    /** Theme simple short length label */
    @NotNull
    @Column(name = "f_lib_theme")
    private String name;
  
    //...
  }
  </code></pre>
  
  <p>NOTE : The SQL sequence "SEQ_NUM_THEME" is generated and associated to F_NUM_THEME in the SQL script.</p>
  
  <p>EDIT 2 :</p>
  
  <p>As Lesiak pointed out, my error might be coming from my DataSource being replace by the annotation <code>@DataJpaTest</code>. By carefully watching the logs, I found some interesting lines.</p>
  
  <pre><code>[INFO ] 2019-05-24 16:22:20.341 [main] org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceBeanFactoryPostProcessor.process(TestDatabaseAutoConfiguration.java:106) : Replacing 'dataSource' DataSource bean with embedded version
  [INFO ] 2019-05-24 16:22:20.346 [main] org.springframework.beans.factory.support.DefaultListableBeanFactory.registerBeanDefinition(DefaultListableBeanFactory.java:824) : Overriding bean definition for bean 'dataSource' with a different definition: replacing [Root bean: class [null]; scope=; abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=org.springframework.boot.autoconfigure.jdbc.DataSourceConfiguration$Hikari; factoryMethodName=dataSource; initMethodName=null; destroyMethodName=(inferred); defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]] with [Root bean: class [org.springframework.boot.test.autoconfigure.jdbc.TestDatabaseAutoConfiguration$EmbeddedDataSourceFactoryBean]; scope=; abstract=false; lazyInit=false; autowireMode=0; dependencyCheck=0; autowireCandidate=true; primary=false; factoryBeanName=null; factoryMethodName=null; initMethodName=null; destroyMethodName=null]
  [INFO ] 2019-05-24 16:22:21.050 [main] org.springframework.jdbc.datasource.embedded.EmbeddedDatabaseFactory.initDatabase(EmbeddedDatabaseFactory.java:189) : Starting embedded database: url='jdbc:h2:mem:4ef27020-c472-4be9-a9af-73f5e0175846;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=false', username='sa'
  </code></pre>
  
  <p>We can see on the last line that my url connection <code>jdbc:h2:mem:db</code> has been replaced by <code>jdbc:h2:mem:4ef27020-c472-4be9-a9af-73f5e0175846</code>. This might be why I'm getting some <code>Table not found</code> errors.</p>