软件测试——NextDay测试

Posted Starzkg

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了软件测试——NextDay测试相关的知识,希望对你有一定的参考价值。

源代码

试题描述

NextDay是一个简单的日期计算器,计算给定日期的下一天的具体日期。如给定2020年1月1日,返回2020年1月2日;给定2020年1月31日,返回2020年2月1日。主要要求考察学生对日期边界以及异常处理的测试的能力。

测试代码

package io.shentuzhigang.demo.test.nextday;

import org.hamcrest.CoreMatchers;
import org.junit.Assert;
import org.junit.Test;

import static org.junit.Assert.assertThat;

/**
 * @author ShenTuZhiGang
 * @version 1.0.0
 * @email 1600337300@qq.com
 * @date 2021-05-23 13:37
 */
public class NextDayTests {

    // 有效
    @Test
    public void testDate01() {
        Date date, result, expected;
        date = new Date(2, 29, 2016);
        result = NextDay.nextDay(date);
        result.printDate();
        expected = new Date(3, 1, 2016);
        Assert.assertEquals(result, expected);

        date = new Date(1, 28, 2017);
        result = NextDay.nextDay(date);
        result.printDate();
        expected = new Date(1, 29, 2017);
        Assert.assertEquals(result, expected);

        date = new Date(1, 31, 2017);
        result = NextDay.nextDay(date);
        result.printDate();
        expected = new Date(2, 1, 2017);
        Assert.assertEquals(result, expected);

        date = new Date(4, 30, 2017);
        result = NextDay.nextDay(date);
        result.printDate();
        expected = new Date(5, 1, 2017);
        Assert.assertEquals(result, expected);

        date = new Date(12, 31, 2017);
        result = NextDay.nextDay(date);
        result.printDate();
        expected = new Date(1, 1, 2018);
        Assert.assertEquals(result, expected);

        date = new Date(12, 31, -1);
        result = NextDay.nextDay(date);
        result.printDate();
        expected = new Date(1, 1, 1);
        Assert.assertEquals(result, expected);


        // fixed 方法覆盖
        String s = date.toString();

        // fixed 代码行覆盖
        boolean b = date.equals(null);
        Year year = new Year(2021);
        b = year.equals(null);
        Month month = new Month(5, year);
        b = month.equals(null);
        Day day = new Day(21, month);
        b = day.equals(null);

    }

    // 抛出异常
    @Test
    public void testDate02() {
        Object[][] exp = {
                {31, 12, -1, "Not a valid month"},
                {3, 1, 0, "Not a valid month"},
                {-1, 1, 1901, "Not a valid month"},
                {-1, 1, 1901, "Not a valid month"},
                {13, 1, 1901, "Not a valid month"},
                {1, -1, 1901, "Not a valid day"},
                {2, 30, 2016, "Not a valid day"},
                {2, 29, 2017, "Not a valid day"},
                {3, 32, 2017, "Not a valid day"},
                {4, 31, 2017, "Not a valid day"},
                {4, 32, 2017, "Not a valid day"}
        };
        for (Object[] e : exp) {
            try {
                Date date = new Date((int) e[0], (int) e[1], (int) e[2]);
                Date d = NextDay.nextDay(date);
            } catch (IllegalArgumentException ex) {
                assertThat(ex.getMessage(), CoreMatchers.containsString((String) e[3]));
            }
        }
    }
}

测试结果

在这里插入图片描述

参考文章

幕测平台_使用Junit测试异常,测试NextDay函数

以上是关于软件测试——NextDay测试的主要内容,如果未能解决你的问题,请参考以下文章

黑盒测试等价划分实例

CTS测试CtsWindowManagerDeviceTestCases模块的testShowWhenLockedImeActivityAndShowSoftInput测试fail项解决方法(代码片段

;~ 小部分AutoHotkey源代码片段测试模板2019年10月9日.ahk

如何在片段着色器中进行自定义模板测试

typescript Angular 2测试片段。代码库https://developers.livechatinc.com/blog/category/programming/angular-2/

typescript Angular最终版本的Angular 2测试片段。代码库https://developers.livechatinc.com/blog/category/programming