使用H2数据库的Spring Boot测试

15
我正在尝试在测试中使用带有H2数据库的Spring Boot API运行测试,但是在运行测试时,系统使用主资源中的application.properties而不是测试中的。我尝试将文件命名为application-test.properties,并在测试类中使用注释@ActiveProfiles("test"),但这没有起作用(尝试将其放入main/resource和test/resource中)。现在我不知道该尝试什么了。
我的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>
2个回答

30

Spring Boot首先总是会加载application.properties文件,如果存在application-{profile}的情况下,它会覆盖应用中父级(application.properties)的某些属性。更多信息请参考Spring Boot文档

你需要拥有main/resource/application.propertiestest/resource/application-test.properties两个文件(不是test目录下的application.properties),以及@ActiveProfiles("test")注解。然后它就能工作了。 如果你发现它无法工作,请检查运行应用程序的类路径。 例如,使用Idea + Maven则为target目录;使用Idea + Gradle则为build目录。


1
你说过:“在这种情况下,最后一个会覆盖父级的某些属性”,这就是问题所在,还有关于名称applicatio-test.properties的问题。在application-test中,我没有为spring.datasource.driver-class-name设置值,现在它可以工作了。 - user2831852
对我来说,那正是问题所在。测试环境的属性文件放置在main/resources/文件夹中。谢谢! - vbknez

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

然后,在您的测试类中添加以下注释:

@ActiveProfiles("test")

这将有效,因为在Spring Boot中我们可以有多个配置文件,所以我们创建了一个名为test的配置文件。

请尝试适当地格式化您的答案中的代码部分。 - dspencer

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接