JUnit主要有两个版本,版本3和版本4,版本3主要是3.8,使用反射机制,版本4使用注解
1、引入JUnit框架:
在项目上右键,在弹出菜单上选择:build path——>configure build path...——>Java Build Path,在右面选择Libraries标签页,然后点击Add Library,选在JUnit,点击next,选择版本,finish完成导入。
2、使用Junit的最佳实践:
1)新建一个名为test的source folder,用于存放测试类源代码
3)测试类的命名规则:加入目标类是Calculator,那么测试类应该命名为:TestCalculator或者是CalculatorTest。
通过build path——>configure build path...——>Java Build Path,点击source标签页,在下面可以看到.class文件的保存路径:junit/bin
3、Junit:单元测试不是为了证明您是对的,而是为了证明您没有错。
4、测试用例(Test Case)是单元测试的一个很重要的方面。
5、单元测试主要是用来判断程序的执行结果与自己期望的结果是否一致。
6、任何测试类都要继承于TestCase父类,在junit3.8中,测试方法需要满足如下原则:
1)、public的 2)、void的 3)、无方法参数 4)、方法名称必须以test开头
7、Test Case之间一定要保持完全的独立性,不允许出现任何依赖关系。我们不能依赖测试方法的执行顺序。
DRY(Don't Repeat Yourself)。
8、关于setUp()与tearDown()方法的执行顺序:
1)setUp 2)testAdd 3)tearDown
import junit.framework.Assert; import junit.framework.TestCase; /** * 测试类必须要继承于TestCase父类 * 在junit3.8中,测试方法需要满足如下原则: * 1、public的 * 2、void的 * 3、无方法参数 * 4、方法名称必须以test开头 * @author zdy */ public class CalculatorTest extends TestCase { public void testAdd() { Calculator cal = new Calculator(); int result = cal.add(3, 2); Assert.assertEquals(5, result); } public void testSubtract() { Calculator cal = new Calculator(); int result = cal.subtract(3, 2); Assert.assertEquals(1, result); } public void testMultiply() { Calculator cal = new Calculator(); int result = cal.multiply(3, 2); Assert.assertEquals(6, result); } public void testDivide() { Calculator cal = new Calculator(); int result = cal.divide(3, 2); Assert.assertEquals(2, result); } }
对上诉代码进行重构,使用setUp和tearDown方法
import junit.framework.Assert; import junit.framework.TestCase; /** * 测试类必须要继承于TestCase父类 * 在junit3.8中,测试方法需要满足如下原则: * 1、public的 * 2、void的 * 3、无方法参数 * 4、方法名称必须以test开头 * @author zdy */ public class CalculatorTest extends TestCase { private Calculator cal; public void setUp() throws Exception { cal = new Calculator(); } public void tearDown() throws Exception { System.out.println("teardown"); } public void testAdd() { int result = cal.add(3, 2); Assert.assertEquals(5, result); } public void testSubtract() { int result = cal.subtract(3, 2); Assert.assertEquals(1, result); } public void testMultiply() { int result = cal.multiply(3, 2); Assert.assertEquals(6, result); } public void testDivide() { int result = cal.divide(3, 2); Assert.assertEquals(2, result); } }
将重复的代码归并到一起。
测试源代码抛异常的情况
import junit.framework.Assert; import junit.framework.TestCase; /** * 测试类必须要继承于TestCase父类 * 在junit3.8中,测试方法需要满足如下原则: * 1、public的 * 2、void的 * 3、无方法参数 * 4、方法名称必须以test开头 * @author zdy */ public class CalculatorTest extends TestCase { private Calculator cal; public void setUp() throws Exception { cal = new Calculator(); } public void tearDown() throws Exception { System.out.println("teardown"); } public void testAdd() { int result = cal.add(3, 2); Assert.assertEquals(5, result); } public void testSubtract() { int result = cal.subtract(3, 2); Assert.assertEquals(1, result); } public void testMultiply() { int result = cal.multiply(3, 2); Assert.assertEquals(6, result); } public void testDivide() { int result = 0; try { result = cal.divide(3, 2); } catch (Exception e) { e.printStackTrace(); Assert.fail(); } Assert.assertEquals(1, result); } public void testDivideByZero() { Throwable tx = null; try { cal.divide(3, 1); Assert.fail(); } catch(Exception e) { tx = e; } Assert.assertEquals(Exception.class,tx.getClass()); Assert.assertEquals("除数不能为0", tx.getMessage()); } }
对int数组求最大值的测试
public class Largest { public int getLargest(int[] array) throws Exception { if(null == array || 0 == array.length) { throw new Exception("数组不能为空"); } int result = array[0]; for(int i = 0;i < array.length;i++) { if(result < array[i]) { result = array[i]; } } return result; } } import junit.framework.Assert; import junit.framework.TestCase; public class LargestTest extends TestCase { private Largest largest; public void setUp() throws Exception { largest = new Largest(); } public void testGetLargest() { int[] array = {1,6,-1,-20,23,43}; int result = 0; try { result = largest.getLargest(array); } catch(Exception e) { Assert.fail("测试失败"); } Assert.assertEquals(43, result); } public void testGetLargest2() { Throwable tx = null; int[] array = {}; try { largest.getLargest(array); Assert.fail("测试失败"); } catch(Exception e) { tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("数组不能为空", tx.getMessage()); } public void testGetLargest3() { Throwable tx = null; int[] array = null; try { largest.getLargest(array); Assert.fail("测试失败"); } catch(Exception e) { tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("数组不能为空", tx.getMessage()); } }
junit自带了run方法运行测试单元
public static void main(String[] args) { junit.textui.TestRunner.run(CalculatorTest.class); junit.awtui.TestRunner.run(CalculatorTest.class); }
9、一个栈类的测试
栈类:MyStack
public class MyStack { private String[] elements; private int nextIndex; public MyStack() { elements = new String[100]; nextIndex = 0; } public void push(String element) throws Exception { if(100 ==nextIndex) { throw new Exception("数组越界异常"); } elements[nextIndex++] = element; } public String pop() throws Exception { if(0 == nextIndex) { throw new Exception("下标越界异常"); } return elements[--nextIndex]; } public void delete(int n) throws Exception { if(nextIndex -n < 0) { throw new Exception("下标越界异常"); } nextIndex -= n; } public String top() throws Exception { if(0 == nextIndex) { throw new Exception("下标越界异常"); } return elements[nextIndex - 1]; } }
栈的测试类:MyStackTest
import junit.framework.Assert; import junit.framework.TestCase; public class MyStackTest extends TestCase { private MyStack myStack; public void setUp() throws Exception { myStack = new MyStack(); } public void testPush() { try { myStack.push("hello world"); } catch (Exception e) { Assert.fail(); } String result = null; try { result = myStack.pop(); } catch (Exception e) { } Assert.assertEquals("hello world", result); } public void testPush2() { for(int i = 0; i< 100;i++) { try { myStack.push(i + ""); } catch (Exception e) { Assert.fail(); } } for(int i = 0; i < 100; i++) { String result = null; try { result = myStack.pop(); } catch (Exception e) { } Assert.assertEquals((99 - i) + "",result); } } public void testPush3() { Throwable tx = null; try { for(int i =0;i<=100;i++) { myStack.push(i + ""); } Assert.fail(); } catch (Exception e) { tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("数组越界异常", tx.getMessage()); } public void testPop() { try { myStack.push("hello world"); } catch (Exception e) { } String result = null; try { result = myStack.pop(); } catch (Exception e) { Assert.fail(); } Assert.assertEquals("hello world", result); } public void testPop2() { Throwable tx = null; try { myStack.pop(); Assert.fail(); } catch (Exception e) { tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("下标越界异常", tx.getMessage()); } public void testPop3() { Throwable tx = null; try { myStack.push("hello"); } catch (Exception e) { } try { myStack.pop(); myStack.pop(); Assert.fail(); } catch (Exception e) { tx =e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("下标越界异常", tx.getMessage()); } public void testTop() { try { myStack.push("hello"); } catch (Exception e) { } String result = null; try { result = myStack.pop(); } catch (Exception e) { Assert.fail(); } Assert.assertEquals("hello", result); } public void testTop2() { Throwable tx = null; try { myStack.top(); Assert.fail(); } catch (Exception e) { tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("下标越界异常", tx.getMessage()); } public void testDelete() { try { for(int i = 0; i < 10; i++) { myStack.push(i + ""); } myStack.delete(10); } catch (Exception e) { Assert.fail(); } } public void testDelete2() { Throwable tx = null; try { for(int i = 0; i < 10; i++) { myStack.push(i + ""); } myStack.delete(11); Assert.fail(); } catch (Exception e) { tx = e; } Assert.assertNotNull(tx); Assert.assertEquals(Exception.class, tx.getClass()); Assert.assertEquals("下标越界异常", tx.getMessage()); } }