Java黑盒測試之nextDate函數(shù)測試
(2)掌握對測試用例進(jìn)行優(yōu)化設(shè)計方法。
二、實(shí)驗(yàn)內(nèi)容日期問題
測試以下程序:該程序有三個輸入變量month、day、year(month、day和year均為整數(shù)值,并且滿足:1≤month≤12、1≤day≤31和1900≤year≤2050),分別作為輸入日期的月份、日、年份,通過程序可以輸出該輸入日期在日歷上隔一天的日期。例如,輸入為2004 年11月30日,則該程序的輸出為2004年12月1日。
(1)劃分等價類,按照等價類劃分法設(shè)計測試用例;
(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.Beforeorg.junit.Afterorg.junit.BeforeClassorg.junit.AfterClassorg.junit.Ignoreorg.junit.runner.RunWithorg.junit.runners.Suite.SuiteClassesorg.junit.runners.Parameterized.Parameters三、實(shí)驗(yàn)要求
(1)根據(jù)題目要求編寫測試用例;
(2)準(zhǔn)備nextDate函數(shù),使用Junit4測試執(zhí)行測試;
(3)撰寫實(shí)驗(yàn)報告。
四、實(shí)驗(yàn)過程(1)根據(jù)題目要求編寫測試用例
1)劃分等價類并編號
輸入數(shù)據(jù) 有效等價類 無效等價類 年 (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時,29<day(8) 30 (18) year為非閏年 month=2時,28<day(9) 31 (19) month=1,3,5,7,8,10,12時,31<day(20) month=4,6,9,11時,30<day(21) day>31(22) 其他輸入2)為有效等價類設(shè)計測試用例
序號 測試數(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)為每一個無效等價類至少設(shè)計一個測試用例
序號 輸入數(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)為intint 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 = {// 有效等價類{'2016','2','29','下一天是2016年3月1日!'},{'2017','1','28','下一天是2017年1月29日!'},{'2017','1','31','下一天是2017年2月1日!'},{'2017','4','30','下一天是2017年5月1日!'},// 無效等價類{'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ā)生故障的原因是程序沒有先判斷接收的三個參數(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)容請搜索好吧啦網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持好吧啦網(wǎng)!
相關(guān)文章:
1. 利用CSS3新特性創(chuàng)建透明邊框三角2. jsp+mysql實(shí)現(xiàn)網(wǎng)頁的分頁查詢3. ASP中解決“對象關(guān)閉時,不允許操作。”的詭異問題……4. React優(yōu)雅的封裝SvgIcon組件示例5. 使用css實(shí)現(xiàn)全兼容tooltip提示框6. IE6/IE7/IE8/IE9中tbody的innerHTML不能賦值的完美解決方案7. CSS可以做的幾個令你嘆為觀止的實(shí)例分享8. 詳解盒子端CSS動畫性能提升9. ASP.NET泛型三之使用協(xié)變和逆變實(shí)現(xiàn)類型轉(zhuǎn)換10. ASP.NET Core實(shí)現(xiàn)中間件的幾種方式
