(五)如何写测试类
要討論的類:先UML定義需要的方法?--->?ADT偽代碼,確認操作和設(shè)計?---->?考慮特殊情況?--->?實現(xiàn)為Java接口(注意寫好注釋)?---->?寫好用來測試的Java語句,更好的理解定義的方法?--->?實現(xiàn)核心方法?----->?測試核心方法?--->?實現(xiàn)其他方法及測試。
分析ArrayBagDemo:
/*** A demostration of the class ArrayBag* @author Administrator**/ public class ArrayBagDemo {public static void main(String[] args) {String[] contentsOfBag = {"A", "A", "B", "A", "C", "A"};// Tests on an empty bagBagInterface<String> aBag = new ArrayBag<>(contentsOfBag.length);System.out.println("Testing an initially empty bag:");testIsEmpty(aBag, true);String[] testStrings1 = {"", "B"};testFrequency(aBag, testStrings1);testContains(aBag, testStrings1);testRemove(aBag, testStrings1);// Adding stringsSystem.out.println("Adding " + contentsOfBag.length + " strings to an initially empty bag "+ "with the capacity to hold more than " + contentsOfBag.length + " strings:");testAdd(aBag, contentsOfBag);// Tests on a bag that is not emptytestIsEmpty(aBag, false);String[] testStrings2 = {"A", "B", "C", "D", "A"};testFrequency(aBag, testStrings2);testContains(aBag, testStrings2);// Removing stringsString[] testStrings3 = {"", "B", "A", "C", "Z"};testRemove(aBag, testStrings3);System.out.println("\nClearing the bag:");aBag.clear();testIsEmpty(aBag, true);displayBag(aBag);}// Tests the method add.public static void testAdd(BagInterface<String> aBag, String[] content) {System.out.println("Adding ");for(int index = 0; index < content.length; index++) {aBag.add(content[index]);System.out.print(content[index] + " ");} // end for System.out.println();displayBag(aBag);} // end testAddprivate static void testRemove(BagInterface<String> aBag, String[] tests) {for (int index = 0; index < tests.length; index++) {String aString = tests[index];if (aString.equals("") || (aString == null)) {// test remove()System.out.println("\nRemoving a string from the bag:");String removedString = aBag.remove();System.out.println("remove() returns " + removedString);}else {// test remove(aString)System.out.println("\nRemoving \"" + aString + "\" from the bag:");boolean result = aBag.remove(aString);System.out.println("remove(\"" + aString + "\") returns " + result);} // end if displayBag(aBag);} // end for } // end testRemove// Tests the method toArray while displaying the bag.private static void displayBag(BagInterface<String> aBag) {System.out.println("The bag contains " + aBag.getCurrentSize() +" string(s), as follows:");Object[] bagArray = aBag.toArray();for (int index = 0; index < bagArray.length; index++) {System.out.print(bagArray[index] + " ");} // end for System.out.println();} // end diaplayBag// Tests the method contains.private static void testContains(BagInterface<String> aBag, String[] tests) {System.out.println("\nTesting the method contains:");for (int index = 0; index < tests.length; index++) {String aString = tests[index];if (!aString.equals("") && (aString != null)) {System.out.println("Does this bag contain " + tests[index] +" ? " + aBag.contains(aString));} // end if} // end for} // end testContains// Tests the method getFrequencyOfprivate static void testFrequency(BagInterface<String> aBag, String[] tests) {System.out.println("\nTesting the method getFrequencyOf:");for (int index = 0; index < tests.length; index++) {String aString = tests[index];if (!aString.equals("") && (aString != null)) { System.out.println("In this bag, the count of " + tests[index] +" is " + aBag.getFrequencyOf(aString));} // end if} // end for} // end testFrequency// Tests the method isEmpty// correctResult indicates what isEmpty should return.private static void testIsEmpty(BagInterface<String> aBag, boolean correctResult) {System.out.println("Testing isEmpty with ");if(correctResult) {System.out.println("an empty bag:");}else {System.out.println("a bag that is not empty:");} // end if System.out.print("isEmpty finds the bag ");if(correctResult && aBag.isEmpty()) {System.out.println("empty: OK.");}else if(correctResult) {System.out.println("not empty, but it is empty: ERROR.");}else if(!correctResult && aBag.isEmpty()) {System.out.println("empty, but it is not empty: ERROR.");}else {System.out.println("not empty: OK.");} // if else System.out.println();} // end testIsEmpty } View Code(1)判空isEmpty(): Boolean
設(shè)計函數(shù)testIsEmpty,將包和是否真正為空的事實作為參數(shù),利用isEmpty函數(shù)和事實對bag做判斷。
(2)測試getFrequencyOf
利用包和字符串數(shù)組做參數(shù),判斷數(shù)組中的元素在包中出現(xiàn)的次數(shù)并輸出
(3)測試contains函數(shù)
參數(shù)為包和數(shù)組,判斷包中是否包含數(shù)組中的元素并輸出
(4)測試remove
將包和測試數(shù)組作為參數(shù),如果當前數(shù)組元素是空串或是null,則測試remove(),否則用remove(T),最后再輸出包中所有元素
(5)測試add
同樣將包和測試數(shù)組作為參數(shù),依次將數(shù)組中元素添加進包中,再輸出包中所有元素
(6)都要使用的輸出包中元素的函數(shù)displayBag
(7)main函數(shù)中:
首先測試空包:判空、測試數(shù)組中元素的個數(shù)、是否包含數(shù)組中元素、移除元素 ---> 向空包中添加元素,測試add函數(shù)?---> 當包不為空時,繼續(xù)測試:判空、元素個數(shù)、是否包含等函數(shù)?---> 從包中移除元素,測試remove函數(shù)?----> 將包清空,通過判空可以測試clear()
(8)總結(jié)
各個測試方法中均為將函數(shù)執(zhí)行后輸出包中元素,對于判空的測試需要使用事實條件繼續(xù)對函數(shù)返回值進行詳細判斷說明。main函數(shù)測試時,先從空包開始測試除add以外的函數(shù),再向包添加元素測試add,隨后在包不為空的情況下繼續(xù)測試所有函數(shù),最后情況,通過判空測試clear函數(shù)。
轉(zhuǎn)載于:https://www.cnblogs.com/datamining-bio/p/9630373.html
總結(jié)
- 上一篇: 【Nginx服务优化与防盗链】
- 下一篇: 【实用】教你如何改造 zblog MIP