Giter Site home page Giter Site logo

shadowwalkerzh / embedded-db-demo Goto Github PK

View Code? Open in Web Editor NEW
1.0 1.0 0.0 7 KB

Make your Java project's DAO unit test independent, it neither depending on specific environment, nor needing to install database instances locally. (including MYSQL, MONGODB, REDIS).

Java 100.00%
java maven databse db mysql mongodb redis unittest unit-test embedded embedded-db spring-boot springboot

embedded-db-demo's Introduction

embedded-db-demo

该项目主要是实现Java项目的DAO层独立进行单元测试,不依赖其它环境,也不需要本地安装数据库实例,主要包括的常用数据库有:MYSQL,MONGODB, REDIS,项目基于Spring Boot + JPA实现。

  • embedded mongodb单元测试mongodb步骤:
    • 需要添加依赖
    <dependency>
       <groupId>de.flapdoodle.embed</groupId>
       <artifactId>de.flapdoodle.embed.mongo</artifactId>
       <version>2.0.0</version>
       <scope>test</scope>
    </dependency>
    <dependency>
       <groupId>cz.jirutka.spring</groupId>
       <artifactId>embedmongo-spring</artifactId>
       <version>1.3.1</version>
    </dependency>
    
    • 创建MongoTemplate实例,可以设置名称,根据业务中的自定义名称来相应的mock.有两种方式可以实现,一是加一个mongo template的定义configuration,另一种是继承mongo配置的抽象类,并重写mongo和getDatabaseName方法。
    • 在测试类中加载test mongo的测试类即可,如:@SpringBootTest(classes = {ApplicationRunner.class, UnitTestMongoConfig.class})
  • h2行单元测试mysql步骤:
    • 添加依赖
    <dependency>
        <groupId>com.h2database</groupId>
        <artifactId>h2</artifactId>
        <version>1.4.193</version>
        <scope>test</scope>
    </dependency>
    
    • 在测试类上加上注解即可mock mysql: @AutoConfigureTestDatabase(connection = EmbeddedDatabaseConnection.H2)
  • embedded redis 单元测试redis:
    • 添加依赖
    <dependency>
        <groupId>com.github.kstyrc</groupId>
        <artifactId>embedded-redis</artifactId>
        <version>0.6</version>
        <scope>test</scope>
    </dependency>
    
    • 自定义redis的测试配置,UnitTestRedisConfig
    • 在测试类中加载配置:@SpringBootTest(classes = {ApplicationRunner.class, UnitTestRedisConfig.class}),在要测试完成后关闭redis服务。

embedded-db-demo's People

Contributors

shadowwalkerzh avatar

Stargazers

 avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.