在Spring中配置特定的内存数据库以进行测试

Ill*_*lSc 38 spring spring-test spring-data spring-test-dbunit spring-boot

如何配置我的Spring Boot应用程序,以便在运行单元测试时,它将使用内存数据库,如H2/HSQL,但是当我运行Spring Boot应用程序时,它将使用生产数据库[Postgre/MySQL]?

San*_*jay 51

Spring配置文件可用于此目的.这将是一种特定的方式:

有环境特定的属性文件:

application.properties:

spring.profiles.active: dev
Run Code Online (Sandbox Code Playgroud)

application-dev.properties

spring.jpa.database: MYSQL
spring.jpa.hibernate.ddl-auto: update

spring.datasource.url: jdbc:mysql://localhost:3306/dbname
spring.datasource.username: username
spring.datasource.password: password
Run Code Online (Sandbox Code Playgroud)

application-test.properties

spring.jpa.database: HSQL
Run Code Online (Sandbox Code Playgroud)

同时拥有MySQLH2驱动程序pom.xml,如下所示:

<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <scope>runtime</scope>
</dependency>

<dependency>
    <groupId>org.hsqldb</groupId>
    <artifactId>hsqldb</artifactId>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)

最后但并非最不重要,使用注释测试类@ActiveProfiles("test").


ron*_*ash 29

另一种方法是将注释添加@AutoConfigureTestDatabase到测试类中.我的测试通常如下所示:

@RunWith(SpringRunner.class)
@DataJpaTest
@AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
public class MyRepositoryTest {

    @Autowired
    MyRepository repository;

    @Test
    public void test() throws Exception {
        // Tests...
    }
}
Run Code Online (Sandbox Code Playgroud)


Ste*_*oll 6

@Sanjay有一种说法,但我发现它令人困惑.你也可以只有一个production你在生产时启用的配置文件,例如:

spring.jpa.hibernate.ddl-auto: update
spring.datasource.url: jdbc:mysql://localhost:3306/dbname
spring.datasource.username: username
spring.datasource.password: password
Run Code Online (Sandbox Code Playgroud)

并且不要指定任何其他内容.如果在test范围中添加嵌入式数据库,它将在您的测试中可用.如果使用默认配置文件运行测试(无需任何自定义),它将找不到任何数据库信息(因为它们存储在production配置文件中).在这种情况下,它将尝试查找嵌入式数据库并为您启动它.如果由于某种原因需要更多自定义,那么您可以使用application-test.properties(您需要添加ActiveProfiles("test")到测试中).


dem*_*iak 5

如果构建的简单解决方案maven:只需将application.properties文件放在下面src/test/resources并根据需要进行编辑以进行测试.

Spring(Boot)Profile机制是一个非常强大的工具,在范围上超越了"在测试时间和运行时间之间交换设置".虽然,显然,正如所示,它也可以做到:)


jar*_*sik 5

最简单的解决方案:

1)在src / main / resources中具有application.properties(生产配置):

spring.datasource.url=jdbc:mysql://localhost:3306/somedb
spring.datasource.username=root
spring.datasource.password=password
spring.datasource.driverClassName=com.mysql.jdbc.Driver
spring.jpa.database-platform = org.hibernate.dialect.MySQL5Dialect
Run Code Online (Sandbox Code Playgroud)

和带有HSQL配置的application-test.properties,如:

spring.jpa.hibernate.ddl-auto = create-drop
spring.jpa.database = HSQL
spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.HSQLDialect
spring.datasource.driverClassName = org.hsqldb.jdbcDriver
spring.datasource.url= jdbc:hsqldb:mem:scratchdb
spring.datasource.username = sa
spring.datasource.password =
Run Code Online (Sandbox Code Playgroud)

2)如果尚未添加HSQL依赖关系,请在pom.xml中添加它。

3)用@ActiveProfiles(“ test”)注释您的测试类。

就我而言,就像魅力一样。


147*_*.3k 5

使用@SpringBootTest 魔法,您只需要进行以下两项更改即可。

  1. 在 pom.xml 中添加“h2”测试依赖
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <scope>test</scope>
</dependency>
Run Code Online (Sandbox Code Playgroud)
  1. 使用@AutoConfigureTestDatabase
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MySpringBootApplication.class)
@AutoConfigureTestDatabase
public class SpringBootTest{

    @Autowired
    private RequestRepository requestRepository;
}
Run Code Online (Sandbox Code Playgroud)

现在测试中使用的所有 spring jpa bean/存储库都将使用 h2 作为后备数据库。

2019-04-26 13:13:34.198 INFO 28627 --- [主要]beddedDataSourceBeanFactoryPostProcessor:用嵌入式版本替换'dataSource'DataSource bean

2019-04-26 13:13:34.199 INFO 28627 --- [main] osbfsDefaultListableBeanFactory:覆盖 bean 'dataSource' 的 bean 定义

2019-04-26 13:13:36.194 INFO 28627 --- [main] osjdeEmbeddedDatabaseFactory :启动嵌入式数据库:url='jdbc:h2:mem:2784768e-f053-4bb3-ab88-edda34956891;DBY_CLOSE=ON假',用户名='sa'

注意:我仍然在“application.properties”中定义了“spring-jpa”属性,并且我没有使用任何配置文件。@ AutoConfigureTestDatabase将使用测试默认值 AutoConfigureTestDatabase.Replace 覆盖现有的 jpa 配置。