执行测试用例。如图所示。
测试结果:红色,测试失败。
(5)修改测试用例:DivideTest.java。具体代码如下所示。
package com.test;
import static org.junit.Assert.*;
import org.junit.Before;
import org.junit.Test;
public class DivideTest {
private static Divide div=new Divide();
@Before
public void init()
{
div.setResult(10);
}
@Test
public void testDivide() {
div.divide(10);
assertEquals(1, div.getResult());
}
@Test(expected=Exception.class)
public final void dividezero()
{
div.divide(0);
}
}
注意:测试方法必须使用注解 org.junit.Test 修饰。 测试方法必须使用 public void 修饰,而且不能带有任何参数。测试方法在JUnit4.X中没有要求,但是为了使得命名意义,一般推荐采用“test”+“被测试方法”的命名规则。assertEquals 是由 JUnit 提供的一系列判断测试结果是否正确的静态断言方法(位于类 org.junit.Assert 中)之一,我们使用它将执行结果 result 和预期值“result”进行比较,来判断测试是否成功。
新测试用例运行后的测试结果如下图所示图所示。
绿色的进度条提示我们,测试运行通过了。但现在就宣布代码通过了单元测试还为时过早。记住:你的单元测试代码不是用来证明你是对的,而是为了证明你没有错。因此单元测试的范围要全面,比如对边界值、正常值、错误值得测试;对代码可能出现的问题要全面预测,而这也正是需求分析、详细设计环节中要考虑的。
三、编写测试代码
(1)应用JUnit对类WordDealUtil编写测试代码
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* 对名称、地址等字符串格式的内容进行格式检查
* 或者格式化的工具类
*/
public class WordDealUtil {
/**
* 将Java对象名称(每个单词的头字母大写)按照
* 数据库命名的习惯进行格式化
* 格式化后的数据为小写字母,并且使用下划线分割命名单词
* 例如:employeeInfo 经过格式化之后变为 employee_info
* @param nameJava对象名称
*/
public static String wordFormat4DB(String name){
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(name);
StringBuffer sb = new StringBuffer();
while(m.find()){
m.appendReplacement(sb, "_"+m.group());
}
return m.appendTail(sb).toString().toLowerCase();
}
}
(2)添加测试代码。
import static org.junit.Assert.assertEquals;
import org.junit.Test;
public class TestWordDealUtil {
//测试wordFormat4DB正常运行的情况
@Test public void wordFormat4DBNormal(){
String target = "employeeInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_info", result);
}
}
推荐每编写完一个测试方法,则执行”run”
看测试结果,测试结果通过:
(3)继续添加测试代码,并运行看测试结果。
public class TestWordDealUtil {
//测试 null 时的处理情况
@Test public void wordFormat4DBNull(){
String target = null;
String result = WordDealUtil.wordFormat4DB(target);
assertNull(result);
}
//测试空字符串的处理情况
@Test public void wordFormat4DBEmpty(){
String target = "";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("", result);
}
//测试当首字母大写时的情况
@Test public void wordFormat4DBegin(){
String target = "EmployeeInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_info", result);
}
//测试当尾字母为大写时的情况
@Test public void wordFormat4DBEnd(){
String target = "employeeInfoA";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_info_a", result);
}
//测试多个相连字母大写时的情况
@Test public void wordFormat4DBTogether(){
String target = "employeeAInfo";
String result = WordDealUtil.wordFormat4DB(target);
assertEquals("employee_a_info", result);
}
}
(4)分析测试结果
测试 null 时的处理情况与测试当首字母大写时的情况出现问题,需要修改源代码
(6)修改代码为:
public class WordDealUtil {
public static String wordFormat4DB(String name){
if(name == null){
return null;
}
Pattern p = Pattern.compile("[A-Z]");
Matcher m = p.matcher(name);
StringBuffer sb = new StringBuffer();
while(m.find()){
if(m.start() != 0)
m.appendReplacement(sb, "_"+m.group());
}
return m.appendTail(sb).toString().toLowerCase();
}
}
测试结果:
至此,现在的代码已经比较稳定。
本文内容不用于商业目的,如涉及知识产权问题,请权利人联系51Testing小编(021-64471599-8017),我们将立即处理