Spring 中的测试 使用数据库 H2 启动

Tests in Spring Boot with database H2

我正在尝试 运行 在 Spring 引导 api 上测试 H2 数据库,但是,当尝试 运行 测试时,系统是在主要资源中使用 application.properties 而不是测试。 我尝试将文件命名为 application-test.properties 并在测试 class 中使用注释 @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

我的test/resource/application.属性:

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

我的测试 class 只是 运行s:

@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 - 构建目录。

在同一目录中创建另一个名为 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

然后将以下注释添加到您的测试中 类:

@ActiveProfiles("test")

这会起作用,因为在 spring 引导中我们可以有多个配置文件,因此我们正在创建一个带有测试名称的配置文件。