【问题标题】:Tests in Spring Boot with database H2在 Spring Boot 中使用数据库 H2 进行测试
【发布时间】:2019-05-17 03:28:06
【问题描述】:

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

我的 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

我的测试/资源/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-boot tdd h2


    【解决方案1】:

    首先是 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 - 构建目录。

    【讨论】:

    • 您说“在这种情况下,最后一个将覆盖父级的一些属性”,这就是名称 applicatio-test.properties 的问题。在应用程序测试中,我没有为spring.datasource.driver-class-name 设置值,现在可以工作了。
    • 对我来说,这正是问题所在。将测试环境的属性文件放在 main/resources/ 文件夹中。谢谢!
    【解决方案2】:

    在同一目录下创建另一个名为application-test.properties的应用程序文件,内容如下,无需在test下创建:

    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 Boot 中我们可以有多个配置文件,因此我们正在创建一个带有测试名称的配置文件。

    【讨论】:

    • 请尝试适当地格式化您的答案的代码部分。
    猜你喜欢
    • 2019-02-02
    • 1970-01-01
    • 2019-04-01
    • 1970-01-01
    • 2022-01-04
    • 2020-07-10
    • 2015-10-09
    • 2020-12-25
    • 2017-07-11
    相关资源
    最近更新 更多