当前位置:首页 > 软件测试实验-JUnit单元测试
public static String wordFormat4DB(String name){ Pattern p = Pattern.compile(\Matcher m = p.matcher(name); StringBuffer sb = new StringBuffer(); } } while(m.find()){ } return m.appendTail(sb).toString().toLowerCase(); m.appendReplacement(sb, \(2) 添加测试代码。 import static org.junit.Assert.assertEquals; import org.junit.Test; public class TestWordDealUtil { } 推荐每编写完一个测试方法,则执行”run”,看测试结果,结果应该是通过的。
测试结果通过:
//测试wordFormat4DB正常运行的情况 @Test public void wordFormat4DBNormal(){ } String target = \String result = WordDealUtil.wordFormat4DB(target); assertEquals(\
(3) 继续添加测试代码,并运行看测试结果。
public class TestWordDealUtil {
//测试 null 时的处理情况
@Test public void wordFormat4DBNull(){ }
String target = null;
String result = WordDealUtil.wordFormat4DB(target); assertNull(result);
//测试空字符串的处理情况
@Test public void wordFormat4DBEmpty(){ }
//测试当首字母大写时的情况 @Test public void wordFormat4DBegin(){ }
String target = \
String result = WordDealUtil.wordFormat4DB(target);
assertEquals(\
String target = \
String result = WordDealUtil.wordFormat4DB(target); assertEquals(\
//测试当尾字母为大写时的情况 @Test public void wordFormat4DBEnd(){ }
//测试多个相连字母大写时的情况
String target = \
String result = WordDealUtil.wordFormat4DB(target);
assertEquals(\
}
@Test public void wordFormat4DBTogether(){ }
String target = \
String result = WordDealUtil.wordFormat4DB(target);
assertEquals(\
再次运行测试。很遗憾,JUnit 运行界面提示我们有两个测试情况未通过测试——当首字母大写时得到的处理结果与预期的有偏差,造成测试失败(failure);而当测试对 null 的处理结果时,则直接抛出了异常——测试错误(error)。显然,被测试代码中并没有对首字母大写和 null 这两种特殊情况进行处理。
图18 JUnit测试运行结果
(4) 修改测试代码,直到测试通过。
修改以后的代码:
测试结果:
【实验小结】
通过本次实验掌握了Junit单元测试的环境配置,以及基本操作步骤,学习
到了JInit单元测试的作用以及如何修改错误,对以后进行软件测试方面收获非常大。经过这次理论学习,明白了要求掌握的知识对于我今后的作用。这让我明确了以后学习的目标,在不断学习软件编程的同时,也应该继续软件测试的深入学习。
共分享92篇相关文档