Sunday, October 23, 2016

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.jarhamcrest-core-1.3.jar,这样你就可以使用JUnit编写单元测试代码了.


三、简单的例子
    记得在几乎每本语言教学书上都能找到HelloWorld这个入门代码。今天在这里,我们也从一个简单到根本不用单元测试的例子入手。四则运算。
    第一步:建立项目引用junit-4.11.jarhamcrest-core-1.3.jar
    第二步:编写Calculator类,代码如下:
[java] view plain copy

  1.   
  2. public class Calculator {   
  3.   
  4.     public int plus(int x, int y) {  
  5.         return x + y;  
  6.     }  
  7.   
  8.     public int subtraction(int x, int y) {  
  9.         return x - y;  
  10.     }  
  11.   
  12.     public int multiplication(int x, int y) {  
  13.         return x * y;  
  14.     }  
  15.   
  16.     public int division(int x, int y) {  
  17.         return x / y;  
  18.     }  
  19.   
  20. }   
    第三步:编写单元测试类,代码如下:
[java] view plain copy
  1.  
  2.   
  3. import static org.junit.Assert.*; //注意这边,静态导入  
  4.   
  5. import org.junit.Ignore;  
  6. import org.junit.Test;  
  7.   

  8.   
  9. public class TestCalculator {  
  10.   
  11.     @Test  
  12.     public void testPlus() {  
  13.         Calculator cal = new Calculator();  
  14.         assertEquals(cal.plus(55), 10);  
  15.     }  
  16.   
  17.     @Test  
  18.     public void testSubtraction() {  
  19.         Calculator cal = new Calculator();  
  20.         assertEquals(cal.subtraction(55), 0);  
  21.     }  
  22.   
  23.     @Ignore  
  24.     @Test  
  25.     public void testMultiplication() {  
  26.         Calculator cal = new Calculator();  
  27.         assertTrue(cal.multiplication(55) > 20);  
  28.     }  
  29.   
  30.     @Test(expected = java.lang.ArithmeticException.class, timeout = 50)  
  31.     public void testDivision() {  
  32.         Calculator cal = new Calculator();  
  33.         assertEquals(cal.division(80), 4); //大家注意看,除数是0  
  34.     }  
  35. }  

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

但是每个测试方法下都new一个Calculator对象很浪费资源,假如有80个测试方法呢?所以接下来我们要使用@BeforeClass,代码如下:
[java] view plain copy

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