java - 在执行 Spring Boot App 期间测试 application.properties 加载,而不是 main/application.prop
问题描述
我已经通过 Spring Boot 应用程序连接了 MARIA DB (Mysql)(在 src/main/resources/aplication.properties 中给出了 mariadb 的详细信息)
我写了一些基本的测试。在 test/resources/application.properties --> 给出 H2 数据库详细信息。
如果我正在运行测试,它会正确获取 test/resources/application.properties 并连接到 H2 Db 并仅在 H2 中执行测试。
但是当我运行我的 Spring Boot 应用程序时,它也会获取 H2 详细信息。(而不是 MariaDB)
如果我要删除 test/resources/application.properties 然后运行我的 spring boot 应用程序-> 它正在获取正常的 MariaDB 详细信息。
主要是在进行 test/application.properties
我想运行连接 MariaDB 的应用程序,并且我想在 H2 中运行我的测试用例。请帮忙!
我的 main/resources/application.properties :
spring.datasource.url=jdbc:mariadb://MYIP/DBNAME
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driver-class-name=org.mariadb.jdbc.Driver
spring.jpa.hibernate.ddl-auto=update
server.port=8014
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MariaDBDialect
org.hibernate.dialect.Dialect=org.hibernate.dialect.MariaDBDialect
我的 test/resources/application.properties :
spring.datasource.driver-class-name=org.h2.Driver
spring.datasource.url=jdbc:h2:mem:db;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=sa
spring.jpa.hibernate.ddl-auto=create-drop
spring.h2.console.enabled=true
logging.level.com.example.demo=DEBUG
server.port=8090
如果我正在运行主应用程序,它仅在 8090 端口上运行并获取 h2 数据库详细信息(如在 test/app.prop 上配置的那样)。我想在主应用程序运行时应该使用 main/resources/app.prop
我的 pom.xml :
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web-services</artifactId>
<version>2.3.0.RELEASE</version>
</dependency>
<dependency>
<groupId>org.mariadb.jdbc</groupId>
<artifactId>mariadb-java-client</artifactId>
<version>1.5.7</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
<version>1.4.194</version>
</dependency>
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-entitymanager</artifactId>
<version>5.2.9.Final</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
解决方案
如果您在 gradle 项目中,您必须将 h2 作为测试实现包括在内,还要放上您的项目结构和 application.properties 的屏幕截图,以便我们为您提供帮助
testImplementation "com.h2database:h2:$h2Version"
推荐阅读
- reactjs - 类型“未知”.ts(2339) 上不存在属性“appendChild”
- 3d - 查找在网格中形成圆的顶点
- django - 如何强制用户在 django 管理面板中只编辑他们自己的模型记录
- java - 将字节数组转换为二进制文件以发送接收格式 application/octet-stream 的客户端
- delphi-7 - IBObjects TIB_Grid 和 TIB_LookupCombo 显示问题
- html - 标题的兄弟姐妹奇数和偶数的CSS不同背景
- sql - Postgresql tstzrange 交集总和
- python - 使用 OpenCV VideoWriter 保存灰度图像时出现 Gstreamer 错误
- c# - 如果名称空间的网站不再可访问,会发生什么情况?
- amazon-web-services - 如何解决 SAM 模板中的“Transform AWS::Include failed with: The specified S3 object's content should be valid Yaml/JSON”