Java单元测试初试
2016-03-21 10:59
453 查看
在项目中,要负责编写一个用于存储数据的字典,刚开始写,为了便于测试代码正确性,在编写初期,确定了一定要在写方法的同时,编写单元测试。因为在负责前面别人编写的一个项目的维护时,发现了蛮多低级的错误,并且我没有参与此项目的完成,费了很多时间去梳理程序发现bug,因此觉得自己编写时,一定要做好测试这关。因此尝试了一番Junit的使用。如下:
先编写了一个简单的数据字典类,如下。按照需求,后期还会添加从数据库表中读取来获得字典,从hbase获取字典,以及输出字典到文件,
新建一个测试类,继承testcase,重新其中的方法,添加测试用例如下:
run中选择junit test,即可执行测试。
先编写了一个简单的数据字典类,如下。按照需求,后期还会添加从数据库表中读取来获得字典,从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,即可执行测试。
相关文章推荐
- LeetCode : Combination Sum II [java]
- Java并发编程:CopyOnWrite容器的实现
- Java 实现多线程的几种方式汇总
- Java String 字符串 比较 == equals
- 深入理解Java的接口和抽象类
- Java排序算法(八)--基数排序(RadixSort)
- java关键字native、static、final详解
- Spring IOC三种注入方式分析及比较
- 利用java反射机制模拟spring IOC原理的实现
- 详解 java 定时任务
- Java运算符
- spring 注解定时任务
- Java day07 内部类
- Java排序算法(七)--简单选择排序(SelectionSort)
- Java排序算法(六)--冒泡排序(交换排序)
- java只有值传递,不存在引用传递
- java中枚举的实现原理
- Java 中 == 比较的是什么?
- Java之Property-统获取一个应用程序运行的次数
- Spring MVC中Session的正确用法之我见02