Java黑盒測試之nextDate函數(shù)測試
一、實(shí)驗(yàn)?zāi)康?/h2>
(1)掌握應(yīng)用黑盒測試技術(shù)進(jìn)行測試用例設(shè)計(jì)。
(2)掌握對測試用例進(jìn)行優(yōu)化設(shè)計(jì)方法。
二、實(shí)驗(yàn)內(nèi)容
日期問題
測試以下程序:該程序有三個(gè)輸入變量month、day、year(month、day和year均為整數(shù)值,并且滿足:1≤month≤12、1≤day≤31和1900≤year≤2050),分別作為輸入日期的月份、日、年份,通過程序可以輸出該輸入日期在日歷上隔一天的日期。例如,輸入為2004 年11月30日,則該程序的輸出為2004年12月1日。
(1)劃分等價(jià)類,按照等價(jià)類劃分法設(shè)計(jì)測試用例;
(2)編寫nextDate函數(shù);
(3)掌握J(rèn)unit4的用法,使用Junit4測試nextDate函數(shù)。
JUnit4是JUnit框架有史以來的最大改進(jìn),其主要目標(biāo)便是利用Java5的Annotation特性簡化測試用例的編寫。
掌握J(rèn)unit4定義的一些常見Annotations:
org.junit.Test org.junit.Before org.junit.After org.junit.BeforeClass org.junit.AfterClass org.junit.Ignore org.junit.runner.RunWith org.junit.runners.Suite.SuiteClasses org.junit.runners.Parameterized.Parameters
三、實(shí)驗(yàn)要求
(1)根據(jù)題目要求編寫測試用例;
(2)準(zhǔn)備nextDate函數(shù),使用Junit4測試執(zhí)行測試;
(3)撰寫實(shí)驗(yàn)報(bào)告。
四、實(shí)驗(yàn)過程
(1)根據(jù)題目要求編寫測試用例
1)劃分等價(jià)類并編號(hào)
輸入數(shù)據(jù) | 有效等價(jià)類 | 無效等價(jià)類 |
年 | (1) 1900到2050內(nèi)的閏年整數(shù) | (10) year<1900 |
(2) 1900到2050內(nèi)的平年整數(shù) | (11) year>2050 | |
(12) 其他輸入 | ||
月 | (3) 1,3,5,7,8,10,12內(nèi)的整數(shù) | (13) month<1 |
(4) 4,6,9,11內(nèi)的整數(shù) | (14) 12<month | |
(5) 2 | (15) 其他輸入 | |
日 | (6) 1~28 | (16) day<1 |
(7) 29 | (17) year為閏年 month=2時(shí),29<day | |
(8) 30 | (18) year為非閏年 month=2時(shí),28<day | |
(9) 31 | (19) month=1,3,5,7,8,10,12時(shí),31<day | |
(20) month=4,6,9,11時(shí),30<day | ||
(21) day>31 | ||
(22) 其他輸入 |
2)為有效等價(jià)類設(shè)計(jì)測試用例
序號(hào) | 測試數(shù)據(jù) | 期望結(jié)果 | 覆蓋范圍 |
1 | 2016 2 29 | 下一天是2016年3月1日! | (1)(5)(7) |
2 | 2017 1 28 | 下一天是2017年1月29日! | (2)(3)(6) |
3 | 2017 1 31 | 下一天是2017年2月1日! | (2)(3)(9) |
4 | 2017 4 30 | 下一天是2017年5月1日! | (2)(4)(8) |
5 | 2017 12 31 | 下一天是2018年1月1日! | (2)(3)(9) |
3)為每一個(gè)無效等價(jià)類至少設(shè)計(jì)一個(gè)測試用例
序號(hào) | 輸入數(shù)據(jù) | 期望結(jié)果 | 覆蓋范圍 |
6 | 1899 3 1 | 年的值不在指定范圍之內(nèi) | (10) |
7 | 2051 3 1 | 年的值不在指定范圍之內(nèi) | (11) |
8 | 205% 3 1 | 無效的輸入日期! | (12) |
9 | 1901 -1 1 | 月的值不在指定范圍之內(nèi) | (13) |
10 | 1901 13 1 | 月的值不在指定范圍之內(nèi) | (14) |
11 | 1901 1% 1 | 無效的輸入日期! | (15) |
12 | 1901 1 -1 | 日的值不在指定范圍之內(nèi) | (16) |
13 | 2016 2 30 | 日的值不在指定范圍之內(nèi) | (17) |
14 | 2017 2 29 | 日的值不在指定范圍之內(nèi) | (18) |
15 | 2017 3 32 | 日的值不在指定范圍之內(nèi) | (19) |
16 | 2017 4 31 | 日的值不在指定范圍之內(nèi) | (20) |
17 | 2017 4 32 | 日的值不在指定范圍之內(nèi) | (21) |
18 | 2017 4 3% | 無效的輸入日期! | (22) |
(2)編寫nextDate函數(shù),使用Junit4測試執(zhí)行測試
被測代碼
package io.shentuzhigang.demo.blackbox; import java.util.regex.Pattern; /** * @author ShenTuZhiGang * @version 1.0.0 * @date 2021-05-06 15:43 */ public class Date { private static final Pattern pattern = Pattern.compile("^[-\\+]?[\\d]*$"); public static String nextDate(String s_year, String s_month, String s_day) { //檢測是否存在無效字符 if (!(isInteger(s_year) && isInteger(s_month) && isInteger(s_day))) { return "無效的輸入日期!"; } //將字符串轉(zhuǎn)為int int year = Integer.parseInt(s_year); int month = Integer.parseInt(s_month); int day = Integer.parseInt((s_day)); boolean flag = false; if (year < 1900 || year > 2050) { return ("年的值不在指定范圍之內(nèi)"); } else if (month > 12 || month < 1) { return ("月的值不在指定范圍之內(nèi)"); } else if (day > 31 || day < 1) { return ("日的值不在指定范圍之內(nèi)"); } switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: if (day == 31) { day = 1; month = month + 1; } else { day = day + 1; } break; case 4: case 6: case 9: case 11: if (day == 30) { day = 1; month = month + 1; } else if (day == 31) { flag = true; } else { day = day + 1; } break; case 12: if (day == 31) { day = 1; month = 1; year = year + 1; } else { day = day + 1; } break; case 2: { if (((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)) { // 閏年 if (day == 29) { day = 1; month = 3; } else if (day < 29) { day = day + 1; } else { flag = true; // day超過29 } } else { //非閏年 if (day == 28) { day = 1; month = 3; } else if (day < 28) { day = day + 1; } else { flag = true; } } } break; default: } if (year > 2050) { return ("年的值不在指定范圍之內(nèi)"); } else if (flag) { return ("日的值不在指定范圍之內(nèi)"); } else { return ("下一天是" + year + "年" + month + "月" + day + "日!"); } } /** * 判斷輸入字符串是否是整數(shù)型 * * @param str * @return */ public static boolean isInteger(String str) { return pattern.matcher(str).matches(); } }
測試代碼
package io.shentuzhigang.demo.blackbox; import org.junit.Assert; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import java.util.Arrays; import java.util.Collection; /** * @author ShenTuZhiGang * @version 1.0.0 * @date 2021-05-06 15:43 */ @RunWith(Parameterized.class) public class DateTests { private String input1; private String input2; private String input3; private String expected; @Parameters public static Collection<?> prepareData(){ String [][] object = { // 有效等價(jià)類 {"2016","2","29","下一天是2016年3月1日!"}, {"2017","1","28","下一天是2017年1月29日!"}, {"2017","1","31","下一天是2017年2月1日!"}, {"2017","4","30","下一天是2017年5月1日!"}, // 無效等價(jià)類 {"1899","3","1","年的值不在指定范圍之內(nèi)"}, {"2051","3","1","年的值不在指定范圍之內(nèi)"}, {"205%","3","1","無效的輸入日期!"}, {"1901","-1","1","月的值不在指定范圍之內(nèi)"}, {"1901","13","1","月的值不在指定范圍之內(nèi)"}, {"1901","1%","1","無效的輸入日期!"}, {"1901","1","-1","日的值不在指定范圍之內(nèi)"}, {"2016","2","30","日的值不在指定范圍之內(nèi)"}, {"2017","2","29","日的值不在指定范圍之內(nèi)"}, {"2017","3","32","日的值不在指定范圍之內(nèi)"}, {"2017","4","31","日的值不在指定范圍之內(nèi)"}, {"2017","4","32","日的值不在指定范圍之內(nèi)"}, {"2017","4","3%","無效的輸入日期!"} }; return Arrays.asList(object); } public DateTests(String input1,String input2,String input3,String expected){ this.input1 = input1; this.input2 = input2; this.input3 = input3; this.expected = expected; } @Test public void testDate(){ String result = Date.nextDate(input1,input2,input3); Assert.assertEquals(expected,result); } }
測試結(jié)果
五、缺陷分析
1.用例?發(fā)生故障的原因是程序先判斷day為29就變?yōu)?月1日,而不先判斷是否為閏年,于是用例?的輸出結(jié)果為2007-3-1而不是無效輸入日期。
2.用例?發(fā)生故障的原因是程序沒有先判斷接收的三個(gè)參數(shù)是否在指定范圍內(nèi),而是先根據(jù)month進(jìn)行數(shù)據(jù)處理,再判斷處理后的參數(shù)是否在指定范圍內(nèi),用例?的參數(shù)因?yàn)閙onth為3所以直接day+1由0變成1再判斷day在指定范圍內(nèi),所以用例?的輸出結(jié)果為1998-3-1而不是日的值不在指定范圍內(nèi)。
到此這篇關(guān)于Java黑盒測試之nextDate函數(shù)測試的文章就介紹到這了,更多相關(guān)Java nextDate函數(shù)測試內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
IntelliJ IDEA使用快捷鍵重命名項(xiàng)目、變量、文件等方法總結(jié)
今天小編就為大家分享一篇關(guān)于IntelliJ IDEA使用快捷鍵重命名項(xiàng)目、變量、文件等方法總結(jié),小編覺得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來看看吧2018-10-10SpringBoot整合Redis實(shí)現(xiàn)緩存分頁數(shù)據(jù)查詢功能
類似淘寶首頁,這些商品是從數(shù)據(jù)庫中查出來的嗎,答案肯定不是,本文我們就通過一個(gè)案例實(shí)操一下,首頁熱點(diǎn)數(shù)據(jù)怎么放到Redis中去查詢,感興趣的同學(xué)可以參考一下2023-06-06java.lang.UnsupportedClassVersionError錯(cuò)誤的解決辦法(附圖文)
這篇文章主要給大家介紹了關(guān)于java.lang.UnsupportedClassVersionError錯(cuò)誤的解決辦法,"java.lang.UnsupportedClassVersionError"意味著您正在運(yùn)行的Java版本與編譯該類時(shí)使用的Java版本不兼容,需要的朋友可以參考下2023-10-10java WebSocket客戶端斷線重連的實(shí)現(xiàn)方法
在工作中是否會(huì)遇到實(shí)用websocket客戶端連接服務(wù)端的時(shí)候,網(wǎng)絡(luò)波動(dòng),服務(wù)端斷連的情況,本文可以直接使用的斷線重連,感興趣的可以了解一下2021-10-10