如何进行一次性运行@datajpatest对真实数据库而不是带有Spring Boot的内存 我正在使用Spring Boot 1.4.3,并进行了大量用@DataJPatest注释的测试。默认情况下,它们会反对内存数据库。我希望能够运行所有这些AGA ...

问题描述 投票:0回答:3

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
java spring-boot spring-test
3个回答
4
投票
在集成测试类中使用

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


1
投票

用@AutoconFigureTestDatabase(repleast = autoconfiguretestdatabase.replace.none)进行JPA测试 - 它使用嵌入式数据库(H2)禁用,否则:否:

org.springframework.Beans.Factory.BeanCreationException:错误
用名称“ dataSource”创建bean:init方法的调用
失败的;嵌套例外是Java.lang.IllegalStateException:未能
用嵌入式数据库替换数据库进行测试。如果你想要一个
嵌入式数据库请在classPath或Tune上放置一个支持的数据库
@autoconfiguretestdatabase的替换属性。

我的问题是我有多个应用程序。项目和DataJpatest中收集了所有这些应用程序,并覆盖了我的属性用于测试生产。
我想出的最好的事情是指示测试使用特定的应用程序。

1
投票
  1. 对于针对实际数据库运行DataJpatest的,无需用
@ActiveProfiles("postgres")

注释每个单个测试类。您可以简单地将以下内容添加到您的java -Dspring.profiles.active=h2 ... enter image description here中,也可以是特定于个人资料的等效物:

@TestPropertySource(properties = "spring.config.location=classpath:/application.properties") @DataJpaTest class MyRepositoryTest { // test methods }
  1. 这将避免创建一个嵌入式的H2实例,以支持您现有的数据库。
最新问题
© www.soinside.com 2019 - 2025. All rights reserved.