use*_*852 1 tdd h2 spring-boot
我正在尝试在具有H2数据库的Spring Boot api上运行测试中的测试,但是,当尝试运行测试时,系统使用的是主要资源中的application.properties而不是测试。我尝试将文件命名为application-test.properties,并@ActiveProfiles("test")在测试类中使用批注,但这不起作用(先将测试放入main / resource中,然后再放入test / resouce中)现在我不知道该尝试什么。
我的main / resource / apllication.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
Run Code Online (Sandbox Code Playgroud)
我的测试/资源/ 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
Run Code Online (Sandbox Code Playgroud)
我刚刚运行的测试类:
@RunWith(SpringRunner.class)
@SpringBootTest
public class BackEndApplicationTests {
@Test
public void contextLoads() {
}
}
Run Code Online (Sandbox Code Playgroud)
我的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>
Run Code Online (Sandbox Code Playgroud)
小智 9
在同一目录中创建另一个名为 application-test.properties 的应用程序文件,其中包含以下内容,无需在测试下创建:
spring.datasource.url = jdbc:h2:~/testdb;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username = sa
spring.datasource.password =
spring.datasource.driverClassName = org.h2.Driver
Run Code Online (Sandbox Code Playgroud)
然后将以下注释添加到您的测试类中:
@ActiveProfiles("test")
Run Code Online (Sandbox Code Playgroud)
这将起作用,因为在 Spring Boot 中我们可以有多个配置文件,因此我们将使用测试名称创建一个配置文件。
首先进行Spring Boot,然后始终加载application.properties,如果存在的话application-{profile}。在这种情况下,最后一个将覆盖父级的一些属性(application.properties)。在spring-boot文档中找到更多信息。
您应该具有main/resource/application.propertiesand test/resource/application-test.properties(而不是测试目录中的application.properties)+ @ActiveProfiles("test")。然后它将起作用。如果您认为这行不通-请检查正在运行的应用的类路径。例如,idea + maven-目标目录;idea + gradle-构建目录。
| 归档时间: |
|
| 查看次数: |
5525 次 |
| 最近记录: |