dimanche 25 septembre 2016

Testing JPA with Spring Boot 1.4.1 and H2

I'm trying to test my JPA code using H2, my production DB is PostGres.

The error I get right now when I run my test is:

Caused by: java.lang.ClassNotFoundException: org.h2.Driver
    at java.net.URLClassLoader.findClass(URLClassLoader.java:381)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:424)
    at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:357)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:348)

I do have H2 in my pom.xml file:

<dependency>
            <groupId>com.h2database</groupId>
            <artifactId>h2</artifactId>
            <scope>runtime</scope>
        </dependency>
        <dependency>
            <groupId>org.hsqldb</groupId>
            <artifactId>hsqldb</artifactId>
            <scope>runtime</scope>
        </dependency>

Not sure why its not finding the driver.

As an alternative, I tried adding @DataJpaTest, which does get me an H2 but I have not been successful in overriding how its configured resulting in FlyWay complaining about existing versions after the test has been ran more than once.

I also tried putting the following in a application.yml (in test/resource) in conjunction with @DataJpaTest to deal with my FlyWay issue, but its ignored with this annotation or not being loaded:

hibernate:
      ddl-auto: create-drop

I would like to either:

  1. Figure out why the driver is not being loaded
  2. Figure out how to tweak the DataSource created by @DataJpaTest
  3. Figure out a better way to configure my test Data Source

Any tips? How have other people configured a situation where production is one type of DB and testing is down with an embedded one?

Aucun commentaire:

Enregistrer un commentaire