zoukankan      html  css  js  c++  java
  • springboot~Profile开发环境与单元测试用不同的数据库

    期望

    1. 希望开发环境dev用mysql
    2. 单元测试使用本机的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();
      }
    
    }
    
  • 相关阅读:
    源码学习-出差有感
    《java数据结构与算法》系列之“快速排序"
    新征途
    命运总是喜欢开玩笑
    《java数据结构与算法》系列之“简单排序"-冒泡,选择,插入
    秒杀9种排序算法(JavaScript版)
    《进击的巨人》
    Noip2001 提高组 T3
    Noip2011 提高组 Day1 T1 铺地毯 + Day2 T1 计算系数
    Noip2012 提高组 Day1 T1 Vigenère 密码
  • 原文地址:https://www.cnblogs.com/lori/p/9456038.html
Copyright © 2011-2022 走看看