update:
因此,我有一个包含用于连接到本地MySQL数据库的DB属性(我已经用来针对本地MySQL运行应用程序)
然后在软件包中右键单击Intellij,然后选择“在软件包中运行所有测试”。在该运行配置的设置中,我将
@ActiveProfiles("local")
添加到“ VM Options”字段中。
我本来会认为这会在测试过程中激活
application-local.properties
轮廓,但事实并非如此。如果我停止本地MySQL,测试仍然可以正常运行。
在其文档中指出,您可以使用@AutoconFigureTestDatabase(repleast = repleast.none)删除自动配置的H2 DataSource
Https://docs.spring.io/spring-boot/docs/1.4.2.release/referene/reference/html/boot-features-testing.html#boot-features-features-testing-testing-testing-boot-boot-boot-boot-boot-boot-applications-testes-testing-testing-testing-testing-autocon-autoconfig--autoconfigure--jpa-jpa-test-test-test-test-test-test-test-test-test------...-.-------------------- 然后,您还需要在属性中提供DB设置,以免使用您的应用程序属性,例如:
application-local.properties
我将此放在应用程序中。
您可以在同一应用程序中使用MySQL DataSource属性添加配置文件。
application.yml
-Dspring.profiles.active=local
local
或使用VM参数启动TEH容器,为:
# 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
add application.yml(属性),具有JDBC连接到SRC/test/Resources
用@AutoconFigureTestDatabase(repleast = autoconfiguretestdatabase.replace.none)进行JPA测试 - 它使用嵌入式数据库(H2)禁用,否则:否:
org.springframework.Beans.Factory.BeanCreationException:错误 用名称“ dataSource”创建bean:init方法的调用 失败的;嵌套例外是Java.lang.IllegalStateException:未能 用嵌入式数据库替换数据库进行测试。如果你想要一个 嵌入式数据库请在classPath或Tune上放置一个支持的数据库 @autoconfiguretestdatabase的替换属性。
我的问题是我有多个应用程序。项目和DataJpatest中收集了所有这些应用程序,并覆盖了我的属性用于测试生产。我想出的最好的事情是指示测试使用特定的应用程序。
@ActiveProfiles("postgres")
注释每个单个测试类。您可以简单地将以下内容添加到您的java -Dspring.profiles.active=h2 ...
中,也可以是特定于个人资料的等效物:
@TestPropertySource(properties = "spring.config.location=classpath:/application.properties")
@DataJpaTest
class MyRepositoryTest {
// test methods
}