使用数据库H2在Spring Boot中进行测试 [英] Tests in Spring Boot with database H2

查看:187
本文介绍了使用数据库H2在Spring Boot中进行测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to run tests on a Spring Boot api with H2 database in the test, however, when trying to run the tests the system is using the application.properties in the main resource instead of the test. I tried naming the file as application-test.properties and use the annotation @ActiveProfiles("test") in the test class but this did not work (testing putting in the main/resource and then in test/resouce) Now I do not know what to try.

我的main/resource/apllication.properties:

My 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.properties:

My test/resource/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,并始终加载application.properties,然后如果存在application-{profile},则始终加载.在这种情况下,最后一个将覆盖父级的一些属性(application.properties).在 spring-boot文档中找到更多信息.

Spring boot at first and always loads application.properties then if exists application-{profile}. At this case the last one will override some properties from the parent (application.properties). Find more at spring-boot documentation.

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

You should have main/resource/application.properties and test/resource/application-test.properties (not application.properties at the test directory) + @ActiveProfiles("test"). Then it will work. If you think that is doesn't work - check classpath of running app. For example, idea + maven - target directory; idea + gradle - build directory.

这篇关于使用数据库H2在Spring Boot中进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆