首页 > 编程语言 > 详细

java单元测试(Junit)

时间:2018-01-23 20:05:16      阅读:271      评论:0      收藏:0      [点我收藏+]

http://blog.csdn.net/stevenhu_223/article/details/8269157 

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

http://download.csdn.net/detail/stevenhu_223/4884357
   在有些时候,我们需要对我们自己编写的代码进行单元测试(好处是,减少后期维护的精力和费用),这是一些最基本的模块测试。当然,在进行单元测试的同时也必然得清楚我们测试的代码的内部逻辑实现,这样在测试的时候才能清楚地将我们希望代码逻辑实现得到的结果和测试实际得到的结果进行验证对比。

  废话少说,上代码:

   首先创建一个java工程,在工程中创建一个被单元测试的Student数据类,如下:

 

[java] view plain copy
 
  1. package com.phicomme.hu;  
  2.   
  3. public class Student  
  4. {  
  5.   
  6.     private String name;  
  7.     private String sex;  
  8.     private int high;  
  9.     private int age;  
  10.     private String school;  
  11.       
  12.     public Student(String name, String sex ,int high, int age, String school)  
  13.     {  
  14.         this.name = name;  
  15.         this.sex = sex;  
  16.         this.high = high;  
  17.         this.age = age;  
  18.         this.school = school;  
  19.     }  
  20.     public String getName()  
  21.     {  
  22.         return name;  
  23.     }  
  24.     public void setName(String name)  
  25.     {  
  26.         this.name = name;  
  27.     }  
  28.     public String getSex()  
  29.     {  
  30.         return sex;  
  31.     }  
  32.     public void setSex(String sex)  
  33.     {  
  34.         this.sex = sex;  
  35.     }  
  36.     public int getHigh()  
  37.     {  
  38.         return high;  
  39.     }  
  40.     public void setHigh(int high)  
  41.     {  
  42.         this.high = high;  
  43.     }  
  44.     public int getAge()  
  45.     {  
  46.         return age;  
  47.     }  
  48.     public boolean setAge(int age)  
  49.     {  
  50.         if (age >25)  
  51.         {  
  52.             return false;  
  53.         }  
  54.         else  
  55.         {  
  56.             this.age = age;  
  57.             return true;  
  58.         }                 
  59.     }  
  60.       
  61.   
  62.     public String getSchool()  
  63.     {  
  64.         return school;  
  65.     }  
  66.     public void setSchool(String school)  
  67.     {  
  68.         this.school = school;  
  69.     }  
  70.       
  71. }  

 

在eclipse下单元测试这个类:  

 

首先导入Junit包:选中java工程,点击鼠标右键--->选择properties---->在窗口中选Java Build Path---->在右侧点击Add Library---->在弹出的窗口列表中选中Junit---->下一步----->Junit 4(我用的是Junit 4)---->finish

这样Junit 4包就导完了,接下来就是创建测试类:

 

将测试类和被测试类放在不同的包中(也可以放在同一个包中,此处只是为了区别),代码如下:

 

测试类1:

[java] view plain copy
 
  1. package com.phicomme.test;  
  2.   
  3. import com.phicomme.hu.Student;  
  4.   
  5. import junit.framework.TestCase;  
  6.   
  7. public class StudentTest01 extends TestCase  
  8. {  
  9.   
  10.     Student testStudent;  
  11.     //此方法在执行每一个测试方法之前(测试用例)之前调用  
  12.     @Override  
  13.     protected void setUp() throws Exception  
  14.     {  
  15.         // TODO Auto-generated method stub  
  16.         super.setUp();  
  17.         testStudent = new Student("djm", "boy", 178, 24, "华东政法");  
  18.         System.out.println("setUp()");  
  19.     }  
  20.   
  21.     //此方法在执行每一个测试方法之后调用  
  22.     @Override  
  23.     protected void tearDown() throws Exception  
  24.     {  
  25.         // TODO Auto-generated method stub  
  26.         super.tearDown();  
  27.         System.out.println("tearDown()");  
  28.     }  
  29.   
  30.     //测试用例,测试Person对象的getSex()方法  
  31.     public void testGetSex()  
  32.     {  
  33.         assertEquals("boy", testStudent.getSex());  
  34.         System.out.println("testGetSex()");  
  35.     }  
  36.       
  37.     //测试Person对象的getAge()方法  
  38.     public void testGetAge()  
  39.     {  
  40.         assertEquals(24, testStudent.getAge());  
  41.         System.out.println("testGetAge()");  
  42.     }  
  43. }  


测试类2:

 

[java] view plain copy
 
  1. package com.phicomme.test;  
  2.   
  3. import junit.framework.TestCase;  
  4.   
  5. import com.phicomme.hu.Student;  
  6.   
  7. public class StudentTest extends TestCase  
  8. {  
  9.   
  10.     private Student testStudent;  
  11.       
  12.     @Override  
  13.     protected void setUp() throws Exception  
  14.     {  
  15.         // TODO Auto-generated method stub  
  16.         super.setUp();  
  17.         testStudent = new Student("steven_hu", "boy", 170 , 23, "上海理工");  
  18.     }  
  19.   
  20.     @Override  
  21.     protected void tearDown() throws Exception  
  22.     {  
  23.         // TODO Auto-generated method stub  
  24.         super.tearDown();  
  25.     }  
  26.   
  27.     public void testSetage()  
  28.     {  
  29.         assertTrue(testStudent.setAge(21));  
  30.     }  
  31.       
  32.     public void testGetSchool()  
  33.     {  
  34.         //预期值和实际值不一样,测试时出现失败(Failure)  
  35.         assertEquals("南昌大学", testStudent.getSchool());  
  36.     }  
  37.       
  38.     public void testGetName()  
  39.     {  
  40.         assertEquals("hdy", testStudent.getName());  
  41.     }  
  42. }  


当然,如果同时需要一起测试以上这两个测试类,可以通过TestSuite类实现,它相当于是一个套件,可以把所有测试类添进来一起运行测试;

代码如下:

[java] view plain copy
 
  1. package com.phicomme.test;  
  2.   
  3. import com.phicomme.hu.StudentTest02;  
  4.   
  5. import junit.framework.Test;  
  6. import junit.framework.TestSuite;  
  7.   
  8. public class AllTest  
  9. {  
  10.   
  11.     //static PersonTest p = new PersonTest();  
  12.     //static PersonTest p1 = new PersonTest();  
  13.     public static Test suite()  
  14.     {  
  15.         TestSuite suite = new TestSuite("Test for com.phicomme.test");  
  16.         //suite.addTest(p);  
  17.         //suite.addTest(p1);  
  18.         suite.addTestSuite(StudentTest.class);  
  19.         suite.addTestSuite(StudentTest01.class);  
  20.         return suite;  
  21.           
  22.     }  
  23. }  


最后,分别测试以上三个类(选中需要测试的类---->鼠标右键---->Run As---->Junit Test):

 

StudentTest类的测试结果图:

技术分享图片

 

StudentTest01类的测试结果图:

技术分享图片

 

AllTest类的测试结果图:

技术分享图片

 

有关java的测试就讲到这里,希望对大家有帮助,有时间也会接着讲讲有关android的单元测试,和在手机上实现编写一个UI界面替代eclipse如上图中的测试界面;

java单元测试(Junit)

原文:https://www.cnblogs.com/ConfidentLiu/p/8337123.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!