(五)如何写测试类
要讨论的类:先UML定义需要的方法 ---> ADT伪代码,确认操作和设计 ----> 考虑特殊情况 ---> 实现为Java接口(注意写好注释) ----> 写好用来测试的Java语句,更好的理解定义的方法 ---> 实现核心方法 -----> 测试核心方法 ---> 实现其他方法及测试。
分析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 bag BagInterface<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 strings System.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 empty testIsEmpty(aBag, false); String[] testStrings2 = {"A", "B", "C", "D", "A"}; testFrequency(aBag, testStrings2); testContains(aBag, testStrings2); // Removing strings String[] 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 testAdd private 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 getFrequencyOf private 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 }
(1)判空isEmpty(): Boolean
设计函数testIsEmpty,将包和是否真正为空的事实作为参数,利用isEmpty函数和事实对bag做判断。
(2)测试getFrequencyOf
利用包和字符串数组做参数,判断数组中的元素在包中出现的次数并输出
(3)测试contains函数
参数为包和数组,判断包中是否包含数组中的元素并输出
(4)测试remove
将包和测试数组作为参数,如果当前数组元素是空串或是null,则测试remove(),否则用remove(T),最后再输出包中所有元素
(5)测试add
同样将包和测试数组作为参数,依次将数组中元素添加进包中,再输出包中所有元素
(6)都要使用的输出包中元素的函数displayBag
(7)main函数中:
首先测试空包:判空、测试数组中元素的个数、是否包含数组中元素、移除元素 ---> 向空包中添加元素,测试add函数 ---> 当包不为空时,继续测试:判空、元素个数、是否包含等函数 ---> 从包中移除元素,测试remove函数 ----> 将包清空,通过判空可以测试clear()
(8)总结
各个测试方法中均为将函数执行后输出包中元素,对于判空的测试需要使用事实条件继续对函数返回值进行详细判断说明。main函数测试时,先从空包开始测试除add以外的函数,再向包添加元素测试add,随后在包不为空的情况下继续测试所有函数,最后情况,通过判空测试clear函数。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术