[英]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 ...
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.