junit入门

时间:2023-03-09 07:17:33
junit入门

一、简介JUnit

JUnit是一个开源的java单元测试框架。在1997年,由 Erich Gamma 和 Kent Beck 开发完成。这两个牛人中 Erich Gamma 是 GOF 之一;Kent Beck 则在 XP 中有重要的贡献(你觉得眼熟一点都不奇怪)。

       正如常言道:“麻雀虽小,五脏俱全。” JUnit设计的非常小巧,但是功能却非常强大。

什么是单元测试?
    写了个类,要给别人用,会不会有bug?怎么办?测试一下。
    
用main方法测试好不好?不好!
    1.    不能一起运行!
    2.    大多数情况下需要人为的观察输出确定是否正确
    
为什么要进行单元测试?
    1.    重用测试,应付将来的实现的变化。
  2.    提高士气,明确知道我的东西是没问题的。

       下面是JUnit一些特性的总结:

1)         提供的API可以让你写出测试结果明确的可重用单元测试用例

2)       提供了三种方式来显示你的测试结果,而且还可以扩展

3)       提供了单元测试用例成批运行的功能

4)       超轻量级而且使用简单,没有商业性的欺骗和无用的向导

5)       整个框架设计良好,易扩展

对不同性质的被测对象,如Class,Jsp,Servlet,Ejb等,Junit有不同的使用技巧。由于本文的性质,以下仅以Class测试为例。

下面我们就开始学习JUnit!

二、下载JUnit.jar和hamcrest.jar

许多IDE都自带了JUnit,但是并不推荐使用,  我们自己动手下载Jar包(不推荐使用的原因后面会说明)

点击http://www.junit.org可以下载到最新版本的JUnit,目前最新版本是4.11。进入官网选择Download and Install guide,

然后选择Plain-old JAR下的junit.jar,找到最新的4.11版本,下载jar包.
  点击http://code.google.com/p/hamcrest/downloads/list下载最新的hamcrest-1.3.zip,解压.找到hamcrest-core-1.3.jar

然后在项目中引用junit-4.11.jar和hamcrest-core-1.3.jar,这样你就可以使用JUnit编写单元测试代码了.

三、简单的例子

记得在几乎每本语言教学书上都能找到HelloWorld这个入门代码。今天在这里,我们也从一个简单到根本不用单元测试的例子入手。四则运算。

第一步:建立项目引用junit-4.11.jar和hamcrest-core-1.3.jar

第二步:编写Calculator类,代码如下:

  1. package com.zjw.junit4;
  2. public class Calculator {
  3. public int plus(int x, int y) {
  4. return x + y;
  5. }
  6. public int subtraction(int x, int y) {
  7. return x - y;
  8. }
  9. public int multiplication(int x, int y) {
  10. return x * y;
  11. }
  12. public int division(int x, int y) {
  13. return x / y;
  14. }
  15. }

第三步:编写单元测试类,代码如下:

  1. package com.zjw.junit4.test;
  2. import static org.junit.Assert.*; //注意这边,静态导入
  3. import org.junit.Ignore;
  4. import org.junit.Test;
  5. import com.zjw.junit4.Calculator;
  6. public class TestCalculator {
  7. @Test
  8. public void testPlus() {
  9. Calculator cal = new Calculator();
  10. assertEquals(cal.plus(5, 5), 10);
  11. }
  12. @Test
  13. public void testSubtraction() {
  14. Calculator cal = new Calculator();
  15. assertEquals(cal.subtraction(5, 5), 0);
  16. }
  17. @Ignore
  18. @Test
  19. public void testMultiplication() {
  20. Calculator cal = new Calculator();
  21. assertTrue(cal.multiplication(5, 5) > 20);
  22. }
  23. @Test(expected = java.lang.ArithmeticException.class, timeout = 50)
  24. public void testDivision() {
  25. Calculator cal = new Calculator();
  26. assertEquals(cal.division(8, 0), 4); //大家注意看,除数是0
  27. }
  28. }

第四步:测试,在这里,我用的是MyEclipse,在TestCalculator类上右键找到Run As 下的JUnit Test,点击然后就开始测试了

第五步:观察测试结果,在这里我测试都是正确的,我们来分析测试结果和代码:

junit入门

1.         Failure是指测试失败

2.         Error是指测试程序本身出错

3.         由于我在testMultiplication方法上加了@Ignore 所以该方法会被忽略

4.         testDivision为什么报测试异常?

  1. @Test(expected = java.lang.ArithmeticException.class, timeout = 50)

看这个@Test expected后面指定你希望抛出的异常,timeout的意思是 如果测试没有在50ms内完成,那么就算测试失败.

5.       大家有没有觉得在每个测试方法下都new一个Calculator对象很浪费资源,假如有80个测试方法呢?所以接下来我们要使用@BeforeClass,代码如下:

  1. package com.zjw.junit4.test;
  2. import static org.junit.Assert.*;
  3. import org.junit.BeforeClass;
  4. import org.junit.Ignore;
  5. import org.junit.Test;
  6. import com.zjw.junit4.Calculator;
  7. public class TestCalculator {
  8. private static Calculator cal;
  9. @BeforeClass
  10. public static void beforeClass(){ //静态方法
  11. cal=new Calculator();
  12. }
  13. @Test
  14. public void testPlus() {
  15. assertEquals(cal.plus(5, 5), 10);
  16. }
  17. @Test
  18. public void testSubtraction() {
  19. assertEquals(cal.subtraction(5, 5), 0);
  20. }
  21. @Ignore
  22. @Test
  23. public void testMultiplication() {
  24. assertTrue(cal.multiplication(5, 5) > 20);
  25. }
  26. @Test(expected = java.lang.ArithmeticException.class, timeout = 50)
  27. public void testDivision() {
  28. assertEquals(cal.division(8, 0), 4);
  29. }
  30. }

为什么@BeforeClass下的方法必须是静态的?因为它在类初始化之前就运行了 .

为什么需要@BeforeClass,因为有的测试需要在测试之前需要取得一些很耗费时间的资源或者要搭建比较耗时间的环境例如建立数据库连接,搭建数据库连接池.
与之对应的还有@AfterClass,用于释放资源,这边我就不写了.

四、JUnit的Annoation:

1.         @Test: 测试方法

a)        (expected=XXException.class)

b)        (timeout=xxx)

2.         @Ignore: 忽略测试方法

3.         @Before: 每一个测试方法之前运行

4.         @After: 每一个测试方法之后运行

5.         @BeforeClass: 所有测试开始之前运行,别忘了方法是静态的.

6.         @AfterClass: 所有测试结束之后运行

五、注意

a)        测试类放在test包中

b)        类名用TestXXX

c)        方法用test方法名命名

六、总结

本文简单的介绍了JUnit使用的入门知识,以后会讲同时测试多个类以及JUnit4的新特性...

未完待续...