How do I configure my Spring Boot application so that when I run unit tests it will use in-memory database such as H2/HSQL but when I run Spring Boot application it will use production database [Postgre/MySQL] ?推荐答案 Spring配置文件可用于此目的.这将是一种特定的方式:Spring profiles can be used for this. This would be a specific way:具有特定于环境的属性文件:Have environment specific properties files: application.properties :spring.profiles.active: dev application-dev.propertiesspring.jpa.database: MYSQLspring.jpa.hibernate.ddl-auto: updatespring.datasource.url: jdbc:mysql://localhost:3306/dbnamespring.datasource.username: usernamespring.datasource.password: password application-test.propertiesspring.jpa.database: HSQL在pom.xml中同时具有 MySQL 和 H2 驱动程序,如下所示:Have both MySQL and H2 drivers in pom.xml, like this:<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>最后但并非最不重要的一点是,用@ActiveProfiles("test")注释测试类.Last but not the least, annotate Test classes with @ActiveProfiles("test"). 这篇关于在Spring中配置特定的内存数据库以进行测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
07-29 20:41
查看更多