JUnit测试框架使用


1.测试Person类中的方法:

 

   1:  package cn.itcast.junit;
   2:   
   3:  public class Person {
   4:   
   5:      public Person() {
   6:          // TODO Auto-generated constructor stub
   7:      }
   8:      public String run(){
   9:          return "run";    
  10:      }
  11:      public String eat(){
  12:          return "eat";
  13:      }
  14:  }
 
 
   1:  package cn.itcast.junit;
   2:  //利用JUnit测试框架测试程序
   3:   
   4:  import org.junit.Test;
   5:   
   6:  public class PersonTest {
   7:     @Test//需要使用到org.junit包中已定义的注解@Test
   8:     public void runTest(){
   9:         Person p=new Person();
  10:         System.out.println(p.run());
  11:     }
  12:     
  13:     @Test
  14:     public void eatTest(){
  15:         Person p=new Person();
  16:         System.out.println(p.eat());
  17:     }
  18:     /*相对于我们在主方法中测试,如果我想一个方法一个方法测试,
  19:     必须注释不测试的,如果方法特别多,相当费力.
  20:     这里利用JUnit非常方便*/
  21:  }

PersonTest

JUnit

绿条表示测试通过,红条则表示不通过.

2. 注解Before,After,BeforeClass,AfterClass使用:

 

   1:  package cn.itcast.junit;
   2:   
   3:  import org.junit.After;
   4:  import org.junit.AfterClass;
   5:  import org.junit.Before;
   6:  import org.junit.BeforeClass;
   7:  import org.junit.Test;
   8:   
   9:  public class PersonTest3 {
  10:         private Person p=null;
  11:         @BeforeClass
  12:         public static void beforeClass(){//必须为静态方法,只有静态成员才具有随着类的加载而加载的特性
  13:             System.out.println("...随着类的加载而调用并且只调用一次...\n");
  14:         }
  15:         @Before
  16:         public void beforeTest(){
  17:             System.out.println("每个测试方法被测试前调用");
  18:             p=new Person();
  19:         }
  20:         @Test
  21:         public void runTest(){
  22:             
  23:             System.out.println(p+" "+p.run());
  24:         }
  25:         
  26:         @Test
  27:         public void eatTest(){
  28:             
  29:             System.out.println(p+" "+p.eat());
  30:         }
  31:         @After
  32:         public void afterTest(){
  33:             p=null;//防止内存泄漏
  34:             System.out.println("每个测试方法被测试后调用");
  35:         }
  36:         @AfterClass
  37:         public static void afterClass(){
  38:             System.out.println("\n....随着类的加载结束后而调用并且只调用一次...");
  39:         }
  40:  }

测试类中所有方法:

BeforeAfter

3.断言使用:

 

   1:  package cn.itcast.junit;
   2:   
   3:   
   4:   
   5:  import org.junit.Assert;
   6:  import org.junit.Test;
   7:   
   8:  public class PersonTest4 {
   9:     
  10:     private Person p=new Person();
  11:     @Test
  12:     public void testRun(){
  13:         //断言的使用
  14:         Assert.assertEquals("run",p.run());//实际返回值为run,期望值也为run->通过
  15:         //Assert.assertEquals("111",p.run());//实际返回值为run,而期望值为111,->测试不能通过
  16:         Assert.assertArrayEquals(new int[]{1,2,3},new int[]{1,2,4});
  17:          //实际数组对象中的元素为1,2,3 而期望数组对象中的元素为1,2,4->测试不能通过
  18:          
  19:     }
  20:  }
测试将不能通过:
Assert
posted @ 2014-01-17 18:35  伊秋  阅读(538)  评论(0编辑  收藏  举报