Springboot单元测试mysql_springboot~Profile开发环境与单元测试用不同的数据库

期望

希望开发环境dev用mysql

单元测试使用本机的h2数据库

引入依赖

compile('org.springframework.boot:spring-boot-starter-data-jpa')

runtime('com.h2database:h2')

runtime('mysql:mysql-connector-java')

两种环境的配置,默认为dev

spring:

application.name: lind-productCenter

profiles.active: dev

rabbitmq:

host: localhost

port: 5672

username: guest

password: guest

virtual-host: pilipa

server:

port: 9090

---

spring:

profiles: dev

datasource:

url: jdbc:mysql://127.0.0.1:3306/productCenter?useSSL=false&allowPublicKeyRetrieval=true&createDatabaseIfNotExist=true

username: root

password: 123456

driver-class-name: com.mysql.jdbc.Driver

jpa:

database: MYSQL

show-sql: true #显示后台处理的SQL语句

hibernate:

ddl-auto: update #自动检查实体和 数据库 表是否一致,如果不一致则会进行更新数据库表

---

spring:

profiles: test

datasource:

platform: h2

driverClassName: org.h2.Driver

url: jdbc:h2:mem:testdb

jpa:

database-platform: org.hibernate.dialect.H2Dialect

hibernate:

ddl-auto: update

单元测试可以提出一个基类,添加注解即可

@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)

@RunWith(SpringRunner.class)

@ActiveProfiles("test")

public class BaseControllerTest {

@Autowired

protected WebTestClient http;

/**

* action 执行前运行 .

*/

@Before

public void before() {

http = http.mutate()

.responseTimeout(Duration.ofMillis(300000))

.build();

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在开发过程中,我们经常需要配置不同的环境,例如开发环境、测试环境和生产环境等。这些环境的配置通常是不同的,因此我们需要在不同的环境中使用不同的配置文件。 对于使用 Maven 构建项目的 Java 开发人员来说,可以使用 Maven 的 profile 功能来实现不同环境的配置分离。 以下是实现 Spring Boot 生产环境和测试环境配置分离的步骤: 1. 在项目的 pom.xml 文件中添加 profile: ```xml <profiles> <profile> <id>dev</id> <properties> <activatedProperties>dev</activatedProperties> </properties> <activation> <activeByDefault>true</activeByDefault> </activation> </profile> <profile> <id>prod</id> <properties> <activatedProperties>prod</activatedProperties> </properties> </profile> </profiles> ``` 2. 在 src/main/resources 目录下创建两个配置文件: application-dev.properties: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/dev_db spring.datasource.username=root spring.datasource.password=123456 ``` application-prod.properties: ```properties spring.datasource.url=jdbc:mysql://localhost:3306/prod_db spring.datasource.username=root spring.datasource.password=123456 ``` 3. 在 pom.xml 文件中配置 Maven 插件: ```xml <build> <plugins> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-resources-plugin</artifactId> <version>3.1.0</version> <executions> <execution> <id>copy-resources</id> <phase>validate</phase> <goals> <goal>copy-resources</goal> </goals> <configuration> <outputDirectory>${basedir}/target/classes</outputDirectory> <resources> <resource> <directory>src/main/resources</directory> <filtering>true</filtering> <includes> <include>application*.properties</include> </includes> </resource> </resources> <overwrite>true</overwrite> </configuration> </execution> </executions> </plugin> </plugins> </build> ``` 4. 在 Spring Boot 主类中添加 @PropertySource 注解: ```java @SpringBootApplication @PropertySource("classpath:application-${activatedProperties}.properties") public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } } ``` 现在,当我们使用 Maven 打包项目时,可以通过指定不同profile 来选择不同的配置文件。例如,使用以下命令打包生产环境: ``` mvn clean package -Pprod ``` 这将使用 application-prod.properties 配置文件,而不是 application-dev.properties。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值