`

单元测试(一)

阅读更多
package com.ssh.junitTest;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class WordDealUtil {

	 /***
	  *  将 Java 对象名称(每个单词的头字母大写)按照
	  * 数据库命名的习惯进行格式化
	  * 格式化后的数据为小写字母,并且使用下划线分割命名单词
	  * @param name
	  * @return
	  */
	 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.ssh.junitTest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;

import org.junit.Test;

public class TestWordDealUtil {
		 // 测试 wordFormat4DB 正常运行的情况
		 @Test public void wordFormat4DBNormal(){ 
			 String target = "employeeInfo"; 
			 String result = WordDealUtil.wordFormat4DB(target); 
			 //静态断言方法  employee_info 为期望值 employeeInfo为所测试的值
			 assertEquals("employee_info", result); 
		 } 
		 
		 // 测试 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); 
		 } 

		 
		   
		 
}

 

package com.ssh.junitTest;

import static org.junit.Assert.assertEquals;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/***
 * 参数化测试的编写单元测试
 * 1. 为准备使用参数化测试的测试类指定特殊的运行器 org.junit.runners.Parameterized。
 * 2. 为测试类声明几个变量,分别用于存放期望值和测试所用数据。
 * 3. 为测试类声明一个使用注解 org.junit.runners.Parameterized.Parameters 修饰的,返回值为 java.util.Collection 的公共静态方法,并在此方法中初始化所有需要测试的参数对。
 * 4. 为测试类声明一个带有参数的公共构造函数,并在其中为第二个环节中声明的几个变量赋值。
 * 5. 编写测试方法,使用定义的变量作为参数进行测试。
 *
 * @author Administrator
 * @version 1.0, Feb 22, 2011 
 * Copyright 2010 Evergreen International Corp.
 */
@RunWith(Parameterized.class)
public class TestWordDealUtil2 {

	private String expected; 
    
    private String target; 
 
     @SuppressWarnings("unchecked")
	 @Parameters 
     public static Collection words(){ 
         return Arrays.asList(new Object[][]{ 
             {"employee_info", "employeeInfo"},      // 测试一般的处理情况
             {null, null},                           // 测试 null 时的处理情况
             {"", ""},                               // 测试空字符串时的处理情况
             {"employee_info", "EmployeeInfo"},      // 测试当首字母大写时的情况
             {"employee_info_a", "employeeInfoA"},   // 测试当尾字母为大写时的情况
             {"employee_a_info", "employeeAInfo"}    // 测试多个相连字母大写时的情况
         }); 
     } 
 
      /*** 
      * 参数化测试必须的构造函数
      * @param expected     期望的测试结果,对应参数集中的第一个参数
      * @param target       测试数据,对应参数集中的第二个参数
      */ 
     public TestWordDealUtil2(String expected , String target){ 
         this.expected = expected; 
         this.target = target; 
     } 
 
      /*** 
      * 测试将 Java 对象名称到数据库名称的转换
      */ 
     @Test public void wordFormat4DB(){ 
         assertEquals(expected, WordDealUtil.wordFormat4DB(target)); 
     } 
	 
}

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics