Junit框架使用(1)--基本使用

原博客:http://blog.csdn.net/jaune161/article/details/40024577

 JUnit是由 Erich Gamma 和 Kent Beck 编写的一个回归测试框架(regression testing framework)。Junit测试是程序员测试,即所谓白盒测试,因为程序员知道被测试的软件如何(How)完成功能和完成什么样(What)的功能。Junit是一套框架,继承TestCase类,就可以用Junit进行自动测试了。

        JUnit是Java开发中使用最多的测试框架之一。

        本系列文章讲的所有内容都是基于Maven3+JUnit4.11,要使用JUnit只需要将JUnit的jar包引入到项目中即可。

首先写要测试的类

[java]  view plain copy
  1. package com.tiamaes.junit;  
  2.   
  3. import java.util.regex.Matcher;  
  4. import java.util.regex.Pattern;  
  5.   
  6. /** 
  7.  * @author 
  8.  * 
  9.  */  
  10. public class WordDealUtil {  
  11.       
  12.     /** 
  13.      * 将Java对象名称格式化成数据库格式 
  14.      * @param name Java对象名称 
  15.      * @return 
  16.      */  
  17.       
  18.     public static String wordFomat4DB(String name){  
  19.           
  20.           
  21.         Pattern p = Pattern.compile("[A-Z]");  
  22.         Matcher m = p.matcher(name);  
  23.         StringBuffer sb = new StringBuffer();  
  24.           
  25.         while(m.find()){  
  26.             System.out.println(m.group());  
  27.             m.appendReplacement(sb, "_"+m.group());  
  28.         }  
  29.           
  30.         return m.appendTail(sb).toString().toUpperCase();  
  31.     }  
  32. }  
下面写测试用例

[java]  view plain copy
  1. package com.tiamaes.junit;  
  2.   
  3. import static org.junit.Assert.*;  
  4.   
  5. import org.junit.Test;  
  6. /** 
  7.  * WordDealUtil测试用例 
  8.  * @author 
  9.  * 
  10.  */  
  11. public class WordDealUtilTest {  
  12.   
  13.     /** 
  14.      * 正常情况 
  15.      */  
  16.     @Test  
  17.     public void testWordFomat4DB() {  
  18.         String target = "employeeInfo";  
  19.         String result = WordDealUtil.wordFomat4DB(target);  
  20.         assertEquals("EMPLOYEE_INFO", result);  
  21.     }  
  22.       
  23.     /** 
  24.      * 参数为null 
  25.      */  
  26.     @Test  
  27.     public void wordFormat4DBNull(){  
  28.         String result = WordDealUtil.wordFomat4DB(null);  
  29.         assertNull(result);  
  30.     }  
  31.       
  32.     /** 
  33.      * 空字符串 
  34.      */  
  35.     @Test  
  36.     public void wordFormat4DBEmpty(){  
  37.         String result = WordDealUtil.wordFomat4DB("");  
  38.         assertEquals("", result);  
  39.     }  
  40.       
  41.     /** 
  42.      * 首字母大写 
  43.      */  
  44.     @Test  
  45.     public void wordFormat4DBBegin(){  
  46.         String target = "EmployeeInfo";  
  47.         String result = WordDealUtil.wordFomat4DB(target);  
  48.         assertEquals("EMPLOYEE_INFO", result);  
  49.     }  
  50.       
  51.     /** 
  52.      * 尾字母大写 
  53.      */  
  54.     @Test  
  55.     public void wordFormat4DBEnd(){  
  56.         String target = "EmployeeInfoA";  
  57.         String result = WordDealUtil.wordFomat4DB(target);  
  58.         assertEquals("EMPLOYEE_INFO_A", result);  
  59.     }  
  60.       
  61.     /** 
  62.      * 多个大写字母相连 
  63.      */  
  64.     public void wordFormat4DBTogether(){  
  65.         String target = "EmployeeAInfo";  
  66.         String result = WordDealUtil.wordFomat4DB(target);  
  67.         assertEquals("EMPLOYEE_A_INFO", result);  
  68.     }  
  69.   
  70. }  
测试方法只需在方法上加上@Test注释就可以了,assertEquals方法有两个参数第一个是期望值,第二个是实际结果。
运行测试用例之后发现wordFormat4DBNull和wordFormat4DBBegin方法失败,即空参数和首字母大写的没有通过测试,然后修改方法,加上null判断和首字母大写判断,如下

[java]  view plain copy
  1. /** 
  2.  * 将Java对象名称格式化成数据库格式 
  3.  * @param name Java对象名称 
  4.  * @return 
  5.  */  
  6. public static String wordFomat4DB(String name){  
  7.       
  8.     /**********************************************/  
  9.     if(name == nullreturn null;  
  10.     /**********************************************/  
  11.       
  12.     Pattern p = Pattern.compile("[A-Z]");  
  13.     Matcher m = p.matcher(name);  
  14.     StringBuffer sb = new StringBuffer();  
  15.       
  16.     while(m.find()){  
  17.         /**********************************************/  
  18.         if(m.start() != 0) {  
  19.             m.appendReplacement(sb, "_"+m.group());  
  20.         };  
  21.         /**********************************************/  
  22.     }  
  23.       
  24.     return m.appendTail(sb).toString().toUpperCase();  
  25. }  
再运行测试用例,这次测试通过,现在的代码已经比较稳定,可以提供给其他模块使用了。如果对此方法进行了修改或优化,只需要运行一次测试用例就可以知道方法是否正常。

在写测试用例时要尽量考虑到业务中可能出现的情况,是测试用例更加完善。


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值