简体   繁体   English

使用Spring Boot 1.4.1和H2测试JPA

[英]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. 我正在尝试使用H2测试我的JPA代码,我的生产数据库是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: 我的pom.xml文件中有H2:

<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. 作为替代方案,我尝试添加@DataJpaTest,这确实让我得到了H2但是我没有成功地覆盖它的配置如何导致FlyWay在测试运行多次之后抱怨现有版本。

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: 我还尝试将以下内容放在application.yml(在test / resource中)与@DataJpaTest一起处理我的FlyWay问题,但是它被忽略了这个注释或者没有被加载:

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 弄清楚如何调整@DataJpaTest创建的DataSource
  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? 其他人如何配置生产是一种类型的数据库并且测试因嵌入式数据而下​​降的情况?

It turns out I was not using 1.4.1 after all. 事实证明我毕竟不使用1.4.1。 I still had 1.4.0. 我还有1.4.0。 I updated to 1.4.1 and did a mvn clean package. 我更新到1.4.1并做了一个mvn clean包。 Also removed the org.hsqldb dependency. 还删除了org.hsqldb依赖项。 Finally I just used the application.yml in src/main/resources and moved my PostGres dependancy to a profile called 'cloud'. 最后,我在src / main / resources中使用了application.yml,并将PostGres的依赖性移到了名为“cloud”的配置文件中。 I removed all the other configs having to do with H2. 我删除了与H2有关的所有其他配置。 I also removed all my annotations on the JUnit class except for the ones given when the test class is first generated by start.spring.io. 我还删除了JUnit类上的所有注释,除了start.spring.io首次生成测试类时给出的注释。 Everything just worked worked after that. 之后所做的一切都在那之后起作用。 Could be I screwed up something in my configs, or maybe there was some sort of bug in 1.4.0? 可能是我在我的配置中搞砸了什么,或者1.4.0中可能存在某种错误? At any rate I'm back in business :-) 无论如何,我重新开始营业:-)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM