简体   繁体   English

如何使用 Spring Boot 针对真实数据库而不是在内存中一次性运行 @DataJpaTest

[英]How to one-off run @DataJpaTest against real database instead of in-memory with Spring Boot

I am using Spring Boot 1.4.3 and have a whole bunch of tests that are annotated with @DataJpaTest .我正在使用 Spring Boot 1.4.3 并且有一大堆用@DataJpaTest注释的测试。 By default, they run against an in-memory database.默认情况下,它们针对内存数据库运行。 I would like to be able to run all of them against a local MySQL temporarily.我希望能够暂时针对本地 MySQL 运行所有这些。 How can I do this in an easy way?我怎样才能以简单的方式做到这一点?

I have found that I can make it work for one by adding @ActiveProfiles("local") where I have an application-local.properties that points to my local MySQL, but it is just too much work to add that everywhere, run the tests and then remove it again (since I only want to run this manually against MySQL, the CI environment will run against the in memory db).我发现我可以通过添加@ActiveProfiles("local")来使其工作,其中我有一个指向我本地 MySQL 的application-local.properties ,但是在任何地方添加它是太多的工作,运行测试然后再次删除它(因为我只想对 MySQL 手动运行它,CI 环境将针对内存数据库运行)。

I am using Maven if that would matter.如果这很重要,我正在使用 Maven。

UPDATE:更新:

So I have an application-local.properties which contains the db properties to connect to my local MySQL database (Which I use already to run my application against the local MySQL)所以我有一个application-local.properties ,其中包含连接到我的本地 MySQL 数据库的 db 属性(我已经使用它来针对本地 MySQL 运行我的应用程序)

Then I right-click in IntelliJ on a package and select "Run all tests in package".然后我右键单击一个包上的 IntelliJ 并选择“运行包中的所有测试”。 In the settings of that run configuration, I add -Dspring.profiles.active=local to the "VM options" field.在该运行配置的设置中,我将-Dspring.profiles.active=local添加到“VM 选项”字段。

I would have thought that this would activate the local profile during the tests, but it does not.我原以为这会在测试期间激活local配置文件,但事实并非如此。 If I stop the local MySQL, the tests still run fine.如果我停止本地 MySQL,测试仍然运行良好。

In the docs it states that you are able to remove the autoconfigured h2 datasource with @AutoConfigureTestDatabase(replace= Replace.NONE) on the test class https://docs.spring.io/spring-boot/docs/1.4.2.RELEASE/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test .在文档中,它指出您可以在测试类https://docs.spring.io/spring-boot/docs/1.4.2.RELEASE上使用 @AutoConfigureTestDatabase(replace= Replace.NONE) 删除自动配置的 h2 数据源/reference/html/boot-features-testing.html#boot-features-testing-spring-boot-applications-testing-autoconfigured-jpa-test

Also you then need to provide your db setup in properties, so that it does not use your app properties eg:此外,您还需要在属性中提供您的数据库设置,以便它不使用您的应用程序属性,例如:

# Database
spring.datasource.url=jdbc:mariadb://localhost:3303/test
spring.datasource.username=test
spring.datasource.password=test
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=none
spring.jpa.show-sql=true

i put this in application.properties in the test package我把它放在测试包的application.properties中

You can add the profile with the MySQL datasource properties in the same application.properties (or .yml) as:您可以在相同的 application.properties(或 .yml)中添加具有 MySQL 数据源属性的配置文件:

application.yml应用程序.yml

# Existing properties

---
spring:
  profiles: h2
# More h2-related properties

---
spring:
  profiles: postgres
  database:
    driverClassName: org.postgresql.Driver
  datasource:
    url: jdbc:postgresql://localhost:5432/db_dvdrental
    username: user_dvdrental
    password: changeit
  jpa:
    database: POSTGRESQL
    generate-ddl: false
# More postgres-related properties

and either use @ActiveProfiles("postgres") in an integration test class or start teh container using VM argument as:并在集成测试类中使用@ActiveProfiles("postgres")或使用 VM 参数启动容器:

java -Dspring.profiles.active=h2 ...
  1. Add application.yml(properties) with jdbc connection into src/test/resources将带有 jdbc 连接的 application.yml(properties) 添加到 src/test/resources

在此处输入图像描述

  1. Run your JPA test with @AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE) - it disables using embedded database (h2), otherwise :使用 @AutoConfigureTestDatabase(replace= AutoConfigureTestDatabase.Replace.NONE) 运行您的 JPA 测试 - 它禁用使用嵌入式数据库 (h2),否则:

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource': Invocation of init method failed; org.springframework.beans.factory.BeanCreationException:创建名为“dataSource”的bean时出错:调用init方法失败; nested exception is java.lang.IllegalStateException: Failed to replace DataSource with an embedded database for tests.嵌套异常是 java.lang.IllegalStateException: 无法用嵌入式数据库替换 DataSource 以进行测试。 If you want an embedded database please put a supported one on the classpath or tune the replace attribute of @AutoConfigureTestDatabase.如果您想要一个嵌入式数据库,请在类路径中放置一个受支持的数据库或调整 @AutoConfigureTestDatabase 的替换属性。

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

相关问题 使用 @DataJpaTest 的测试未使用嵌入式内存数据库 - Tests with @DataJpaTest are not using embedded in-memory database Spring Boot中的外部配置和一次性测试 - External configuration in Spring Boot and one-off testing 内存数据库配置(HSQLDB),用于Spring启动应用程序中的集成测试 - In-memory database configuration (HSQLDB) for integration testing in spring boot app 使用内存数据库在 Spring Boot 中进行集成测试 - Integration test in Spring Boot using in-memory database 在Spring Boot应用程序中同时使用内存数据库和生产数据库 - Using both in-memory and production database in Spring Boot application 如何在多个 Spring Boot 应用程序之间共享 H2 内存数据库? - How do I share H2 in-memory database across multiple Spring boot applications? 针对内存中集合运行JPQL查询 - Run JPQL query against in-memory collection 如何在 Spring 引导测试中连接到内存中的 HSQLDB 以进行查询 - How to connect to HSQLDB in-memory in Spring Boot tests to make a query Spring Boot DataJpaTest 单元测试恢复到 H2 而不是 mySql - Spring Boot DataJpaTest unit test reverting to H2 instead of mySql 使用Oracle数据库配置的Spring Boot @DataJpaTest无法获取数据 - Spring boot @DataJpaTest configured with Oracle database not fetching data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM