您好,登錄后才能下訂單哦!
這篇文章主要介紹了java聯調生成測試數據工具類的方法是什么的相關知識,內容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇java聯調生成測試數據工具類的方法是什么文章都會有所收獲,下面我們一起來看看吧。
在日常的聯調中,我們經常需要準備一定數量的測試數據,用來配合前端測試。
當然對于簡單的數據類型完全可以通過 JDK 自帶的 Random 類來實現。
但是參數的格式有特殊要求的時候,臨時處理比較麻煩,這個時候就需要借助一些現存的工具類生成測試數據。
import java.math.BigDecimal; import java.text.ParseException; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Random; public class TestDataUtils { //復姓出現的幾率(0--100) private static int surnameProbability = 5; private static Random random = new Random(); private static String familyOneName = "趙錢孫李周吳鄭王馮陳褚衛蔣沈韓楊朱秦尤許何呂施張孔曹嚴華金魏陶姜戚謝鄒喻水云蘇潘葛奚范彭郎魯韋昌馬苗鳳花方俞任" + "袁柳鮑史唐費岑薛雷賀倪湯滕殷羅畢郝鄔安常樂于時傅卞齊康伍余元卜顧孟平黃和穆蕭尹姚邵湛汪祁毛禹狄米貝明臧計成戴宋茅龐熊紀舒屈項祝董粱杜阮" + "席季麻強賈路婁危江童顏郭梅盛林刁鐘徐邱駱高夏蔡田胡凌霍萬柯盧莫房繆干解應宗丁宣鄧郁單杭洪包諸左石崔吉龔程邢滑裴陸榮翁荀羊甄家封芮儲靳邴" + "松井富烏焦巴弓牧隗山谷車侯伊寧仇祖武符劉景詹束龍葉幸司韶黎喬蒼雙聞莘勞逄姬冉宰桂牛壽通邊燕冀尚農溫莊晏瞿茹習魚容向古戈終居衡步都耿滿弘" + "國文東毆沃曾關紅游蓋益桓公晉楚閆"; private static String familyTwoName = "歐陽太史端木上官司馬東方獨孤南宮萬俟聞人夏侯諸葛尉遲公羊赫連澹臺皇甫宗政濮陽公冶太叔申屠公孫慕容仲孫鐘離長孫宇" + "文司徒鮮于司空閭丘子車亓官司寇巫馬公西顓孫壤駟公良漆雕樂正宰父谷梁拓跋夾谷軒轅令狐段干百里呼延東郭南門羊舌微生公戶公玉公儀梁丘公仲公上" + "公門公山公堅左丘公伯西門公祖第五公乘貫丘公皙南榮東里東宮仲長子書子桑即墨達奚褚師吳銘"; private static String boyName = "偉剛勇毅俊峰強軍平保東文輝力明永健世廣志義興良海山仁波寧貴福生龍元全國勝學祥才發武新利清飛彬富順信子杰濤昌成康星光天達" + "安巖中茂進林有堅和彪博誠先敬震振壯會思群豪心邦承樂紹功松善厚慶磊民友裕河哲江超浩亮政謙亨奇固之輪翰朗伯宏言若鳴朋斌梁棟維啟克倫翔旭鵬澤" + "晨辰士以建家致樹炎德行時泰盛雄琛鈞冠策騰楠榕風航弘"; private static String girlName = "秀娟英華慧巧美娜靜淑惠珠翠雅芝玉萍紅娥玲芬芳燕彩春菊蘭鳳潔梅琳素云蓮真環雪榮愛妹霞香月鶯媛艷瑞凡佳嘉瓊勤珍貞莉桂娣葉璧" + "璐婭琦晶妍茜秋珊莎錦黛青倩婷姣婉嫻瑾穎露瑤怡嬋雁蓓紈儀荷丹蓉眉君琴蕊薇菁夢嵐苑婕馨瑗琰韻融園藝詠卿聰瀾純毓悅昭冰爽琬茗羽希寧欣飄育瀅馥" + "筠柔竹靄凝曉歡霄楓蕓菲寒伊亞宜可姬舒影荔枝思麗"; private static String phoneTwoNum = "3578"; private static String[] province = {"河北省", "山西省", "遼寧省", "吉林省", "黑龍江省", "江蘇省", "浙江省", "安徽省", "福建省", "江西省", "山東省", "河南省", "湖北省", "湖南省", "廣東省", "海南省", "四川省", "貴州省", "云南省", "陜西省", "甘肅省", "青海省", "臺灣省",}; private static String[] city = {"安康市", "安慶市", "安順市", "安陽市", "鞍山市", "巴彥淖爾市", "巴中市", "白城市", "白山市", "白銀市", "百色市", "蚌埠市", "包頭市", "寶雞市", "保定市", "保山市", "北海市", "本溪市", "濱州市", "滄州市", "昌都地區", "長春市", "長沙市", "長治市", "常德市", "常州市", "巢湖市", "朝陽市", "潮州市", "郴州市", "成都市", "承德市", "池州市", "赤峰市", "崇左市", "滁州市", "達州市", "大連市", "大慶市", "大同市", "丹東市", "德陽市", "德州市", "定西市", "東莞市", "東營市", "鄂爾多斯市", "鄂州市", "防城港市", "佛山市", "福州市", "撫順市", "撫州市", "阜新市", "阜陽市", "甘南州", "贛州市", "固原市", "廣安市", "廣元市", "廣州市", "貴港市", "貴陽市", "桂林市", "哈爾濱市", "哈密地區", "海北藏族自治州", "海東地區", "海口市", "邯鄲市", "漢中市", "杭州市", "毫州市", "合肥市", "河池市", "河源市", "菏澤市", "賀州市", "鶴壁市", "鶴崗市", "黑河市", "衡水市", "衡陽市", "呼和浩特市", "呼倫貝爾市", "湖州市", "葫蘆島市", "懷化市", "淮安市", "淮北市", "淮南市", "黃岡市", "黃山市", "黃石市", "惠州市", "雞西市", "吉安市", "吉林市", "濟南市", "濟寧市", "佳木斯市", "嘉興市", "嘉峪關市", "江門市", "焦作市", "揭陽市", "金昌市", "金華市", "錦州市", "晉城市", "晉中市", "荊門市", "荊州市", "景德鎮市", "九江市", "酒泉市", "開封市", "克拉瑪依市", "昆明市", "拉薩市", "來賓市", "萊蕪市", "蘭州市", "廊坊市", "樂山市", "麗江市", "麗水市", "連云港市", "遼陽市", "遼源市", "聊城市", "臨滄市", "臨汾市", "臨沂市", "柳州市", "六安市", "六盤水市", "龍巖市", "隴南市", "婁底市", "瀘州市", "呂梁市", "洛陽市", "漯河市", "馬鞍山市", "茂名市", "眉山市", "梅州市", "綿陽市", "牡丹江市", "內江市", "南昌市", "南充市", "南京市", "南寧市", "南平市", "南通市", "南陽市", "寧波市", "寧德市", "攀枝花市", "盤錦市", "平頂山市", "平涼市", "萍鄉市", "莆田市", "濮陽市", "普洱市", "七臺河市", "齊齊哈爾市", "欽州市", "秦皇島市", "青島市", "清遠市", "慶陽市", "曲靖市", "衢州市", "泉州市", "日照市", "三門峽市", "三明市", "三亞市", "汕頭市", "汕尾市", "商洛市", "商丘市", "上饒市", "韶關市", "邵陽市", "紹興市", "深圳市", "沈陽市", "十堰市", "石家莊市", "石嘴山市", "雙鴨山市", "朔州市", "四平市", "松原市", "蘇州市", "宿遷市", "宿州市", "綏化市", "隨州市", "遂寧市", "臺州市", "太原市", "泰安市", "泰州市", "唐山市", "天水市", "鐵嶺市", "通化市", "通遼市", "銅川市", "銅陵市", "銅仁市", "吐魯番地區", "威海市", "濰坊市", "渭南市", "溫州市", "烏海市", "烏蘭察布市", "烏魯木齊市", "無錫市", "吳忠市", "蕪湖市", "梧州市", "武漢市", "武威市", "西安市", "西寧市", "錫林郭勒盟", "廈門市", "咸寧市", "咸陽市", "湘潭市", "襄樊市", "孝感市", "忻州市", "新鄉市", "新余市", "信陽市", "興安盟", "邢臺市", "徐州市", "許昌市", "宣城市", "雅安市", "煙臺市", "延安市", "鹽城市", "揚州市", "陽江市", "陽泉市", "伊春市", "伊犁哈薩克自治州", "宜賓市", "宜昌市", "宜春市", "益陽市", "銀川市", "鷹潭市", "營口市", "永州市", "榆林市", "玉林市", "玉溪市", "岳陽市", "云浮市", "運城市", "棗莊市", "湛江市", "張家界市", "張家口市", "張掖市", "漳州市", "昭通市", "肇慶市", "鎮江市", "鄭州市", "中山市", "中衛市", "舟山市", "周口市", "株洲市", "珠海市", "駐馬店市", "資陽市", "淄博市", "自貢市", "遵義市",}; private static String[] area = {"伊春區", "帶嶺區", "南岔區", "金山屯區", "西林區", "美溪區", "烏馬河區", "翠巒區", "友好區", "新青區", "上甘嶺區", "五營區", "紅星區", "湯旺河區", "烏伊嶺區", "榆次區"}; private static String[] road = {"黃河路", "中原路", "安波路", "新四路", "安汾路", "安福路", "安國路", "安化路", "安瀾路", "安龍路", "安仁路", "安順路", "安亭路", "安圖路", "安業路", "安義路", "安遠路", "鞍山路", "鞍山支路", "澳門路", "八一路", "巴林路", "白城路", "白城南路", "白渡路", "白渡橋", "白蘭路", "白水路", "白玉路", "百安路(方泰鎮)", "百官街", "百花街", "百色路", "板泉路", "半淞園路", "包頭路", "包頭南路", "寶安公路", "寶安路", "寶昌路", "寶聯路", "寶林路", "寶祁路", "寶山路", "寶通路", "寶楊路", "寶源路", "保德路", "保定路", "保屯路", "保屯路", "北艾路",}; private static String[] home = {"金色家園", "耀江花園", "陽光翠竹苑", "東新大廈", "溢盈河畔別墅", "真新六街坊", "和亭佳苑", "協通公寓", "博泰新苑", "菊園五街坊", "住友嘉馨名園", "復華城市花園", "愛里舍花園"}; private static String passwordForm = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789abcdefghijklmnopqrstuvwxyz~!@#$%^&*()_+{}|<>?"; /** * 生成隨機數 */ public static int randomInt() { return random.nextInt(); } /** * 生成隨機數(最大值限制) */ public static int randomInt(int maxNum) { return random.nextInt(maxNum); } /** * 獲取隨機男生姓名 */ public static String getRandomBoyName() { int bodNameIndexOne = randomInt(boyName.length()); int bodNameIndexTwo = randomInt(boyName.length()); return getNameStr(bodNameIndexOne, bodNameIndexTwo, boyName); } /** * 獲取隨機女生姓名 */ public static String getRandomGirlName() { int bodNameIndexOne = randomInt(girlName.length()); int bodNameIndexTwo = randomInt(girlName.length()); return getNameStr(bodNameIndexOne, bodNameIndexTwo, girlName); } /** * 獲取隨機手機號 */ public static String getRandomPhone() { int phoneTwoRandomIndex = randomInt(4); return "1" + phoneTwoNum.substring(phoneTwoRandomIndex, phoneTwoRandomIndex + 1) + (100000000 + randomInt(899999999)); } /** * 獲取隨機qq郵箱 */ public static String getRandomQQEmail() { return ("" + random.nextLong()).substring(10) + "@qq.com"; } /** * 獲取隨機性別 */ public static String getRandomSex() { return randomInt(2) % 2 == 0 ? "男" : "女"; } /** * 獲取隨機年齡 * * @param min 最小年齡 * @param max 最大年齡 * @return */ public static int getRandomAge(int min, int max) { return min + random.nextInt(max - min); } /** * 獲取隨機金額 */ public static Double getRandomMoney(int min, int max) { int randomAge = getRandomAge(min, max); double v = random.nextDouble(); return randomAge + v; } /** * 獲取隨機金額 */ public static BigDecimal getRandomDecimal(Integer min, Integer max) { return getRandomRedPacket(min, max, BigDecimal.ROUND_DOWN); } /** * 獲取金額 * * @param min * @param max * @return */ public static BigDecimal getRandomRedPacket(Integer min, Integer max, int roundingMode) { float minF = min.floatValue(); float maxF = max.floatValue(); //生成隨機數 BigDecimal db = new BigDecimal(Math.random() * (maxF - minF) + minF); //返回保留兩位小數的隨機數。不進行四舍五入 return db.setScale(2, roundingMode); } /** * 獲取隨機住址 */ public static String getRandomAddress() { return province[randomInt(province.length)] + city[randomInt(city.length)] + area[randomInt(area.length)] + road[randomInt(road.length)] + home[randomInt(home.length)]; } /** * 獲取隨機時間 */ public static Date getRandonTime() { Random rndYear = new Random(); int year = rndYear.nextInt(18) + 2000; //生成[2000,2017]的整數;年 Random rndMonth = new Random(); int month = rndMonth.nextInt(12) + 1; //生成[1,12]的整數;月 Random rndDay = new Random(); int Day = rndDay.nextInt(30) + 1; //生成[1,30)的整數;日 Random rndHour = new Random(); int hour = rndHour.nextInt(23); //生成[0,23)的整數;小時 Random rndMinute = new Random(); int minute = rndMinute.nextInt(60); //生成分鐘 Random rndSecond = new Random(); int second = rndSecond.nextInt(60); //秒 String dateStr = year + "-" + month + "-" + Day + " " + hour + ":" + minute + ":" + second; return dateTime(YYYY_MM_DD_HH_MM_SS, dateStr); } /** * 獲取隨機密碼(指定長度) */ public static String getRandomPassword(int length) { StringBuilder springBuilder = new StringBuilder(); for (int i = 0; i < length; i++) { int index = randomInt(passwordForm.length()); springBuilder.append(passwordForm.substring(index, index + 1)); } return springBuilder.toString(); } /** * 獲取隨機密碼(指定長度) */ public static String getRandomPassword() { return getRandomPassword(12); } public static String YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd HH:mm:ss"; public static String toSeconds(Date date) { return format(date, YYYY_MM_DD_HH_MM_SS); } public static String format(Date date, String format) { SimpleDateFormat sdf = new SimpleDateFormat(format); return date == null ? "" : sdf.format(date); } public static final Date dateTime(final String format, final String ts) { try { return new SimpleDateFormat(format).parse(ts); } catch (ParseException e) { throw new RuntimeException(e); } } private static String getNameStr(int bodNameIndexOne, int bodNameIndexTwo, String nameType) { if (randomInt(100) > surnameProbability) { int familyOneNameIndex = randomInt(familyOneName.length()); return familyOneName.substring(familyOneNameIndex, familyOneNameIndex + 1) + nameType.substring(bodNameIndexOne, bodNameIndexOne + 1) + nameType.substring(bodNameIndexTwo, bodNameIndexTwo + 1); } else { int familyTwoNameIndex = randomInt(familyTwoName.length()); familyTwoNameIndex = familyTwoNameIndex % 2 == 0 ? familyTwoNameIndex : familyTwoNameIndex - 1; return familyTwoName.substring(familyTwoNameIndex, familyTwoNameIndex + 2) + nameType.substring(bodNameIndexOne, bodNameIndexOne + 1) + nameType.substring(bodNameIndexTwo, bodNameIndexTwo + 1); } } public static void main(String[] args) { int randomInt = randomInt(); // 獲取隨機數 System.out.println("randomInt = " + randomInt); int randomInt2 = randomInt(8); // 獲取隨機數 System.out.println("randomInt2 = " + randomInt2); String randomBoyName = getRandomBoyName(); // 獲取男士姓名 System.out.println("randomBoyName = " + randomBoyName); String randomGirlName = getRandomGirlName(); // 獲取女士姓名 System.out.println("randomGirlName = " + randomGirlName); String randomPhone = getRandomPhone(); // 獲取手機號碼 System.out.println("randomPhone = " + randomPhone); String randomQQEmail = getRandomQQEmail(); // 獲取qq郵箱 System.out.println("randomQQEmail = " + randomQQEmail); String randomSex = getRandomSex(); // 獲取性別 System.out.println("randomSex = " + randomSex); int randomAge = getRandomAge(0, 100); // 獲取年齡 System.out.println("randomAge = " + randomAge); Double randomMoney = getRandomMoney(0, 100); // 獲取金額double System.out.println("randomMoney = " + randomMoney); BigDecimal randomDecimal = getRandomDecimal(0, 200); // 獲取金額保留兩位小數decimal System.out.println("randomDecimal = " + randomDecimal); String randomAddress = getRandomAddress(); // 獲取地址 System.out.println("randomAddress = " + randomAddress); Date randonTime = getRandonTime(); // 獲取時間 System.out.println("randonTime = " + randonTime); String format = toSeconds(randonTime); // 獲取時間 System.out.println("format = " + format); String randomPassword = getRandomPassword(); // 獲取密碼 System.out.println("randomPassword = " + randomPassword); String randomPassword1 = getRandomPassword(10); // 獲取密碼(10位) System.out.println("randomPassword1 = " + randomPassword1); } }
import com.alibaba.druid.pool.DruidDataSourceFactory; import javax.sql.DataSource; import java.io.FileInputStream; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties; /** * 基于druid數據庫連接池的工具類 */ public class JDBCUtilsByDruid { private static DataSource ds; //在靜態代碼塊完成ds初始化 static { Properties properties = new Properties(); try { properties.load(new FileInputStream("src\\druid.properties")); ds = DruidDataSourceFactory.createDataSource(properties); } catch (Exception e) { e.printStackTrace(); } } //編寫getConnection方法 public static Connection getConnection() throws SQLException { return ds.getConnection(); } //關閉連接,注意:在數據庫連接池技術中,close不是真的斷掉連接 //而是把使用的Connection對象放回連接池 public static void close(ResultSet resultSet, Statement statement, Connection connection) { try { if(resultSet != null) { resultSet.close(); } if(statement != null) { statement.close(); } if(connection != null) { connection.close(); } } catch (SQLException e) { throw new RuntimeException(e); } } }
import com.hspedu.jdbc.utils.JDBCUtils; import org.junit.Test; import java.sql.*; public class JDBCUtilsByDruid_Use { @Test public void testSelect() { System.out.println("使用druid方式完成"); //1.得到連接 Connection connection = null; //2.組織一個sql String sql = "select * from actor where id = ?"; //3.創建PreparedStatement對象 PreparedStatement preparedStatement = null; ResultSet resultSet = null; try { connection = JDBCUtilsByDruid.getConnection(); System.out.println(connection.getClass()); //運行類型 class com.alibaba.druid.pool.DruidPooledConnection preparedStatement = connection.prepareStatement(sql); //給占位符賦值 preparedStatement.setInt(1, 1); //執行 resultSet = preparedStatement.executeQuery(); //遍歷該結果集 while (resultSet.next()) { int id = resultSet.getInt("id"); String name = resultSet.getString("name"); String sex = resultSet.getString("sex"); Date borndate = resultSet.getDate("borndate"); String phone = resultSet.getString("phone"); System.out.println(name + "\t" + sex + "\t" + borndate + "\t" + phone); } } catch (SQLException e) { e.printStackTrace(); } finally { //關閉資源 JDBCUtilsByDruid.close(resultSet, preparedStatement, connection); } } }
關于“java聯調生成測試數據工具類的方法是什么”這篇文章的內容就介紹到這里,感謝各位的閱讀!相信大家對“java聯調生成測試數據工具類的方法是什么”知識都有一定的了解,大家如果還想學習更多知識,歡迎關注億速云行業資訊頻道。
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。