Java单元测试初试

在项目中,要负责编写一个用于存储数据的字典,刚开始写,为了便于测试代码正确性,在编写初期,确定了一定要在写方法的同时,编写单元测试。因为在负责前面别人编写的一个项目的维护时,发现了蛮多低级的错误,并且我没有参与此项目的完成,费了很多时间去梳理程序发现bug,因此觉得自己编写时,一定要做好测试这关。因此尝试了一番Junit的使用。如下:

先编写了一个简单的数据字典类,如下。按照需求,后期还会添加从数据库表中读取来获得字典,从hbase获取字典,以及输出字典到文件,

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Scanner;
import java.util.Map.Entry;

public class IntValueDictionary{
	private HashMap<String, Integer> dic = new HashMap<String, Integer>();
	
	public IntValueDictionary(){}
	/**
	 * Construct dictionary from file in the disk.
	 * @param path The path of the source file.
	 * If the line doesn't  contains a ',', the value will be set as 1000.  
	 */
	public IntValueDictionary(String path){
		try {
			FileReader reader = new FileReader(new File(path));
			Scanner in = new Scanner(reader);
			String line = null;
			String str = null;
			int num = 0;
			boolean singleType = true;
			while(in.hasNextLine()){
				line = in.nextLine();
//				System.out.println(line); 
				if (singleType&&line.contains(",")) {
					singleType = false;
				}
				if (singleType == true) {
					str = line;
					num = 1000;
				} else {
					String[] entry = line.split(",");
					str = entry[0];
					num = Integer.parseInt(entry[1]);
				}
				dic.put(str, num);
			}
			in.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}
	/**
	 * Construct dictionary from HashMap. 
	 */
	public IntValueDictionary(HashMap<String, Integer> dic){
		this.dic = dic;	
	}
	/**
	 * Return a dictionary. 
	 */
	public HashMap<String, Integer> getDic(){
		return dic;
	}
	/**
	 * Check whether the source string contains any word in the dictionary.
	 * @param str The source string.
	 * @return true if source string contains at least one word in the dictionary; otherwise false.
	 */
	public boolean isContainedIn(String str){
		if(str == null)
			return false;
		Iterator<Entry<String, Integer>> iter = dic.entrySet().iterator();
        while(iter.hasNext()){
        	Entry<String, Integer> tempEntry = iter.next();
        	if(str.contains(tempEntry.getKey()))
        		return true;
        }
		return false;
	}
	
	public boolean containsKey(String key) {
		return dic.containsKey(key);
	}
	
	public int getValue(String key) {
		if(dic.get(key) == null)
			return 0;
		return dic.get(key);
	}
	/**
	 * Add item to the dictionary.
	 * @param key The key.
	 * @param value The value.
	 * @return If the key exists,then return false.
	 */
	public boolean addItem(String key,Integer value) {
		if(dic.get(key) == null){
			dic.put(key, value);
			return true;
		}
		return false;
	}
	/**
	 * Add item to the dictionary,If the key exists,then the value will be rewritten.
	 * @param key The key.
	 * @param value The value.
	 */
	public void add(String key,Integer value) {
		dic.put(key, value);
	}
	
}


新建一个测试类,继承testcase,重新其中的方法,添加测试用例如下:

import udb.eda.dictionary.IntValueDictionary;
import junit.framework.TestCase;  

public class IntValueDictionaryTest extends TestCase{
	IntValueDictionary intValueDic;  
    //此方法在执行每一个测试方法之前(测试用例)之前调用  
    @Override  
    protected void setUp() throws Exception  
    {  
        // TODO Auto-generated method stub  
        super.setUp();  
        intValueDic = new IntValueDictionary("E:\\myjava\\test\\123.txt");  
        //System.out.println("setUp()");  
    }   
    //此方法在执行每一个测试方法之后调用  
    @Override  
    protected void tearDown() throws Exception  
    {  
        // TODO Auto-generated method stub  
        super.tearDown();  
        //System.out.println("tearDown()");  
    }    
    //测试用例
    public void testContainsKey()  
    {  
        assertEquals(true, intValueDic.containsKey("a"));
        assertEquals(true, intValueDic.containsKey("d"));  
        assertEquals(true, intValueDic.containsKey("i"));  
        assertEquals(false, intValueDic.containsKey("y"));  
        System.out.println("testContainsKey()");  
    }        
    //测试用例
    public void testGetValue()  
    {  
        assertEquals(1, intValueDic.getValue("a"));
        assertEquals(4, intValueDic.getValue("d"));  
        assertEquals(9, intValueDic.getValue("i"));  
        assertEquals(0, intValueDic.getValue("y"));  
        System.out.println("testGetValue()");  
    } 
  //测试用例
    public void testIsContainedIn()  
    {  
        assertEquals(true, intValueDic.isContainedIn("kkemm"));
        assertEquals(true, intValueDic.isContainedIn("ihpp"));  
        assertEquals(true, intValueDic.isContainedIn("ii"));  
        assertEquals(true, intValueDic.isContainedIn("ppl")); 
        assertEquals(false, intValueDic.isContainedIn("z")); 
        System.out.println("testIsContainedIn()");  
    }
    public void testAddItem()  
    {  
        assertEquals(true, intValueDic.addItem("xy", 22));
        assertEquals(22, intValueDic.getValue("xy")); 
        assertEquals(false, intValueDic.addItem("k", 100));
        assertEquals(11, intValueDic.getValue("k")); 
        System.out.println("testAddItem()");  
    } 
    public void testAdd()  
    {  
    	intValueDic.add("k", 33);
        assertEquals(33, intValueDic.getValue("k")); 
        intValueDic.add("i", 33);
        assertEquals(33, intValueDic.getValue("i")); 
        System.out.println("testAdd()");  
    }
}


run中选择junit test,即可执行测试。





  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值