Java 单元测试基础封装

本文介绍了Java单元测试的基础封装,包括pom文件的jar包依赖,H2数据库工具类的编写,Spring Boot的ApplicationTests基类,ServiceUtils模拟服务启动工具,以及Controller、Service和Dao的测试基类。通过这些基类,可以方便地进行API接口、Service逻辑和服务启动的模拟测试。
摘要由CSDN通过智能技术生成

pom 文件导入 jar 包依赖
<!-- 模拟数据库时使用 -->
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
<dependency>
    <groupId>com.h2database</groupId>
    <artifactId>h2</artifactId>
    <version>1.4.200</version>
    <scope>test</scope>
</dependency>
<!-- 需要启动 redis 服务时使用 -->
<!-- https://mvnrepository.com/artifact/ai.grakn/redis-mock -->
<dependency>
    <groupId>ai.grakn</groupId>
    <artifactId>redis-mock</artifactId>
    <version>0.1.6</version>
    <scope>test</scope>
</dependency>
<!-- 需要使用 zookeeper server 时使用 -->
<!-- https://mvnrepository.com/artifact/org.apache.curator/curator-test -->
<dependency>
    <groupId>org.apache.curator</groupId>
    <artifactId>curator-test</artifactId>
    <version>5.0.0</version>
    <scope>test</scope>
</dependency>

编写 H2 数据库工具类

工具类主要用来执行 sql 语句文件,初始化脚本请在 配置文件中配置 schema

import lombok.extern.slf4j.Slf4j;
import org.h2.tools.RunScript;

import java.io.FileReader;
import java.io.Reader;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;

/**
 * insert description here
 *
 * @author Showa.L
 * @since 2020/6/17 17:16
 */
@Slf4j
public class H2Utils {
   

    private static Connection CONNECTION = null;

    static {
   
        try {
   
            // 加载驱动
            Class.forName("org.h2.Driver");
        } catch (ClassNotFoundException ex) {
   
            log.error("get driver error", ex);
        
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值