JUnit4 学习总结

来源:互联网 发布:unity3d spine2d插件 编辑:程序博客网 时间:2024/05/22 05:14

转载自:http://blog.csdn.net/xiazdong/article/details/7317376

一、JUnit介绍

Junit是 Erich Gamma 和 Kent Beck编写的测试框架,是我们在软件工程所说的白盒测试。

使用也很简单,只需要在Eclipse导入JAR包即可;

下载地址:https://github.com/downloads/KentBeck/junit/junit4.10.zip

 

二、JUnit4和JUnit3的比较

 

JUnit3JUnit4测试类需要继承TestCase不需要继承任何类测试函数约定:public、void、test开头、无参数需要在测试函数前面加上@Test

每个测试函数之前setUp执行

@Before每个测试函数之后tearDown执行@After没有类加载时执行的函数@BeforeClass和@AfterClass

 

三、JUnit4详解

 

1.@Test用来标注测试函数

2.@Before用来标注此函数在每次测试函数运行之前运行

3.@After用来标注此函数在每次测试函数运行之后运行

4.@BeforeClass用来标注在测试开始时运行;

5.@AfterClass 用来标注在测试结束时运行;

6.Assert类中有很多断言,比如assertEquals("期望值","实际值");


代码示例:

Person.java

[java] view plaincopy
  1. package org.xiazdong;  
  2.   
  3. public class Person {  
  4.     private String name;  
  5.     private int age;  
  6.     public String getName() {  
  7.         return name;  
  8.     }  
  9.     public void setName(String name) {  
  10.         this.name = name;  
  11.     }  
  12.     public int getAge() {  
  13.         return age;  
  14.     }  
  15.     public void setAge(int age) {  
  16.         this.age = age;  
  17.     }  
  18.     public Person(String name, int age) {  
  19.         this.name = name;  
  20.         this.age = age;  
  21.     }  
  22.       
  23. }  

PersonTest.java

此测试是用JUnit3测试的;

[java] view plaincopy
  1. package org.xiazdong.junit;  
  2.   
  3. import junit.framework.Assert;  
  4. import junit.framework.TestCase;  
  5.   
  6. import org.xiazdong.Person;  
  7.   
  8. public class PersonTest extends TestCase {  
  9.       
  10.     @Override  
  11.     protected void setUp() throws Exception {  
  12.         System.out.println("setUp");  
  13.     }  
  14.   
  15.     @Override  
  16.     protected void tearDown() throws Exception {  
  17.         System.out.println("tearDown");  
  18.     }  
  19.   
  20.     public void testFun1(){  
  21.         Person p = new Person("xiazdong",20);  
  22.         Assert.assertEquals("xiazdong", p.getName());  
  23.         Assert.assertEquals(20, p.getAge());  
  24.     }  
  25.     public void testFun2(){  
  26.         Person p = new Person("xiazdong",20);  
  27.         Assert.assertEquals("xiazdong", p.getName());  
  28.         Assert.assertEquals(20, p.getAge());  
  29.     }  
  30. }  

PersonTest2.java

此测试是用JUnit4测试的;

[java] view plaincopy
  1. package org.xiazdong.junit;  
  2.   
  3. import junit.framework.Assert;  
  4.   
  5. import org.junit.After;  
  6. import org.junit.AfterClass;  
  7. import org.junit.Before;  
  8. import org.junit.BeforeClass;  
  9. import org.junit.Test;  
  10. import org.xiazdong.Person;  
  11.   
  12. public class PersonTest2 {  
  13.       
  14.     @Before  
  15.     public void before(){  
  16.         System.out.println("before");  
  17.     }  
  18.     @After  
  19.     public void after(){  
  20.         System.out.println("After");  
  21.     }  
  22.     @BeforeClass  
  23.     public void beforeClass(){  
  24.         System.out.println("BeforeClass");  
  25.     }  
  26.     @AfterClass  
  27.     public void afterClass(){  
  28.         System.out.println("AfterClass");  
  29.     }  
  30.     @Test  
  31.     public void testFun1(){  
  32.         Person p = new Person("xiazdong",20);  
  33.         Assert.assertEquals("xiazdong", p.getName());  
  34.         Assert.assertEquals(20, p.getAge());  
  35.     }  
  36.     @Test  
  37.     public void testFun2(){  
  38.         Person p = new Person("xiazdong",20);  
  39.         Assert.assertEquals("xiazdong", p.getName());  
  40.         Assert.assertEquals(20, p.getAge());  
  41.     }  
  42. }