gpt4 book ai didi

spring-boot - 使用数据库 H2 在 Spring Boot 中进行测试

转载 作者:行者123 更新时间:2023-12-03 15:13:41 25 4
gpt4 key购买 nike

我试图在测试中使用 H2 数据库在 Spring Boot api 上运行测试,但是,当尝试运行测试时,系统使用主资源中的 application.properties 而不是测试。
我尝试将文件命名为 application-test.properties 并使用注释 @ActiveProfiles("test")在测试类中,但这不起作用(测试放入主/资源然后放入测试/资源)
现在我不知道该尝试什么。

我的主要/资源/应用程序.properties:

spring.datasource.url=jdbc:mysql://localhost:3306/chamados
spring.datasource.username=root
spring.datasource.password=12345
spring.datasource.driver-class-name=com.mysql.jdbc.Driver

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5Dialect
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect

我的测试/资源/application.properties:
spring.datasource.url=jdbc:h2:mem:testdb
spring.datasource.username=sa
spring.datasource.password=

spring.h2.console.enabled=false

spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.properties.hibernate.format_sql=true

我刚刚运行的测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class BackEndApplicationTests {

@Test
public void contextLoads() {
}

}

我的 pom.xml:
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<java.version>1.8</java.version>
<jwt.version>0.9.1</jwt.version>
</properties>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</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-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-test</artifactId>
<scope>test</scope>
</dependency>

<!-- Autenticação -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>

<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt</artifactId>
<version>${jwt.version}</version>
</dependency>

</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>

最佳答案

Spring 启动首先并始终加载 application.properties 然后如果存在 application-{profile} .在这种情况下,最后一个将覆盖父级 (application.properties) 的一些属性。更多信息请访问 spring-boot documentation .

你应该有 main/resource/application.propertiestest/resource/application-test.properties ( 不是 application.properties 在测试目录 ) + @ActiveProfiles("test") .然后它会起作用。
如果您认为这不起作用 - 检查正在运行的应用程序的类路径。
比如idea+maven-目标目录; idea + gradle - 构建目录。

关于spring-boot - 使用数据库 H2 在 Spring Boot 中进行测试,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53803780/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com