通过判断两个时间段是否重合案例来学习Junit参数化测试(Parameterized)

这个更全面一些

这次来学习一下参数化测试功能,可以用来批量验证数据集

代码如下:

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Arrays;
import java.util.Collection;
import java.util.Date;

/**
 * 判断时间是否有重合  并学习参数化测试方法  依赖 junit 包
 *
 * Creted by Dean on 2019-05-22.
 */
@RunWith(Parameterized.class) //此注解语句必须加在这里
public class DateDuplicateParameterizedTests {

    //此注解需要 加在 返回待测试的 数据集 方法 上
    @Parameterized.Parameters
    //必须为 static 方法,返回类型必须为 Collection
    public static Collection data() {
        String timeStartPostfix = " 00:00:00";
        String timeEndPostfix = " 23:59:59";

        Date oldStartDate = parseDateTime("2019-05-01" + timeStartPostfix);
        Date oldEndDate = parseDateTime("2019-05-31" + timeEndPostfix);
        
        return Arrays.asList(new Object[][]{
                {parseDateTime("2019-04-01" + timeStartPostfix), parseDateTime("2019-05-04" + timeEndPostfix), oldStartDate, oldEndDate, "不通过"},
                {parseDateTime("2019-05-04" + timeStartPostfix), parseDateTime("2019-06-04" + timeEndPostfix), oldStartDate, oldEndDate, "不通过"},
                {parseDateTime("2019-05-01" + timeStartPostfix), parseDateTime("2019-05-15" + timeEndPostfix), oldStartDate, oldEndDate, "不通过"},
                {parseDateTime("2019-05-10" + timeStartPostfix), parseDateTime("2019-05-31" + timeEndPostfix), oldStartDate, oldEndDate, "不通过"},
                {parseDateTime("2019-05-01" + timeStartPostfix), parseDateTime("2019-05-31" + timeEndPostfix), oldStartDate, oldEndDate, "不通过"},
                {parseDateTime("2019-05-10" + timeStartPostfix), parseDateTime("2019-05-20" + timeEndPostfix), oldStartDate, oldEndDate, "不通过"},
                {parseDateTime("2019-04-01" + timeStartPostfix), parseDateTime("2019-06-01" + timeEndPostfix), oldStartDate, oldEndDate, "不通过"},
                {parseDateTime("2019-03-01" + timeStartPostfix), parseDateTime("2019-04-01" + timeEndPostfix), oldStartDate, oldEndDate, "通过"},
                {parseDateTime("2019-06-01" + timeStartPostfix), parseDateTime("2019-07-01" + timeEndPostfix), oldStartDate, oldEndDate, "通过"},
        });
    }

    /** 以下 成员 变量 对应 数据集中的 数据项 */
    private Date newStartDate;
    private Date newEndDate;
    private Date oldStartDate;
    private Date oldEndDate;
    private String expected;

    /** 构造器 参数 需要与 数据集中的 数据项  顺序对应 */
    public DateDuplicateParameterizedTests(Date newStartDate, Date newEndDate, Date oldStartDate, Date oldEndDate, String expected) {
        this.newStartDate = newStartDate;
        this.newEndDate = newEndDate;
        this.oldStartDate = oldStartDate;
        this.oldEndDate = oldEndDate;
        this.expected = expected;
    }

    @Test
    public void checkDateDuplicate(){
        boolean result = ! (newStartDate.after(oldEndDate) || newEndDate.before(oldStartDate));
        Assert.assertEquals(expected, result ? "不通过" : "通过");
    }

    /** 时间解析工具方法 */
    private static Date parseDateTime(String dateStr) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            return format.parse(dateStr);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

}
一图胜千言,代码中的数据集里可能出现的情况如下图

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值