本文主要介绍了如何使用 JUnit 4 提供的各种功能开展有效的单元测试,并通过一个实例演示了如何使用 Ant 执行自动化的单元测试。本文假设读者对 Eclipse 下进行 Java
开发有一定的经验,并了解 Java 5 中的注解(annotation)特性。
引言
JUnit 使这一切变得简单艺术起来。
Erich Gamma 是经典著作《设计模式:可复用面向对象软件的基础》一书的作者之一,并在 Eclipse 中有很大的贡献;Kent Beck 则是一位极限编程(XP)方面的专家和先驱。
JUnit 4 使用 Java 5 中的注解(annotation)使测试变得更加简单。
JUnit 4 初体验
IDE。您可以从 http://www.eclipse.org/ 上下载最新的 Eclipse 版本。
中已经集成了 Ant,但是在撰写本文时,Eclipse 使用的 Ant 版本较低(必需 1.7 或者以上版本),不能很好的支持 JUnit 4。
中使用 JUnit,就不必再下载了。
Build Path”子选项 -> 点选“Add Library…”按钮 -> 在弹出的“Add Library”对话框中选择 JUnit(图1),并在下一页中选择版本 4.1 后点击“Finish”按钮。这样便把 JUnit 引入到当前项目库中了。
图1 为项目添加 JUnit 库
请注意 JDK 的版本
JDK1.4.x 版本上。如果您需要在 JDK1.4.x 版本使用 JUnit 的话,请使用 3.8.1 版本。
根目录下添加一个新目录 testsrc,并把它加入到项目源代码目录中(加入方式见 图2)。
图2 修改项目源代码目录
wordFormat4DB 是专用于处理 Java 对象名称向数据库表名转换的方法(您可以在代码注释中可以得到更多详细的内容)。下面是第一次编码完成后大致情形:
-
package
com.ai92.cooljunit; -
-
import
java.util.regex.Matcher; -
import
java.util.regex.Pattern; -
-
-
public
class WordDealUtil { -
-
-
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(); -
} -
}
-
package
com.ai92.cooljunit; -
-
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); -
} -
}
调用执行被测试方法 WordDealUtil.wordFormat4DB,以判断运行结果是否达到设计预期的效果。需要注意的是,测试方法 wordFormat4DBNormal 需要按照一定的规范书写:
测试方法必须使用 public void 修饰,而且不能带有任何参数。
是由 JUnit 提供的一系列判断测试结果是否正确的静态断言方法(位于类 org.junit.Assert 中)之一,我们使用它将执行结果 result 和预期值“employee_info”进行比较,来判断测试是否成功。
图3 JUnit 运行成功界面
-
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); -
} -
}
null 这两种特殊情况进行处理,修改如下:
-
//修改后的方法wordFormat4DB
-
-
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()).toLowerCase()); -
} -
return m.appendTail(sb).toString().toLowerCase(); -
}
图4 JUnit 运行失败界面
请牢记!