Junit3 junit.framework 单元测试,简单实例说明.

2 篇文章 0 订阅
2 篇文章 0 订阅
     在有些时候,我们需要对我们自己编写的代码进行单元测试(好处是,减少后期维护的精力和费用),这是一些最基本的模块测试。当然,在进行单元测试的同时也必然得清楚我们测试的代码的内部逻辑实现,这样在测试的时候才能清楚地将我们希望代码逻辑实现得到的结果和测试实际得到的结果进行验证对比。
        首先创建一个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( Junit3的jar 用Junit4的jar也可以, 所以不用奇怪Junit3用Junit4的Jar)

这样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"17824"华东政法");  
  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如上图中的测试界面;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:148) at java.util.AbstractList.add(AbstractList.java:108) at org.apache.commons.collections.CollectionUtils.addAll(CollectionUtils.java:688) at com.bosssoft.hr.train.j2se.util.UtilsDemo.method2(UtilsDemo.java:60) at Test1.testUtilsDemo2(Test1.java:56) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) at java.lang.reflect.Method.invoke(Method.java:498) at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:59) at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:12) at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:56) at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:17) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.BlockJUnit4ClassRunner$1.evaluate(BlockJUnit4ClassRunner.java:100) at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:366) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:103) at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:63) at org.junit.runners.ParentRunner$4.run(ParentRunner.java:331) at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:79) at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:329) at org.junit.runners.ParentRunner.access$100(ParentRunner.java:66) at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:293) at org.junit.runners.ParentRunner$3.evaluate(ParentRunner.java:306) at org.junit.runners.ParentRunner.run(ParentRunner.java:413) at org.junit.runner.JUnitCore.run(JUnitCore.java:137) at com.intellij.junit4.JUnit4IdeaTestRunner.startRunnerWithArgs(JUnit4IdeaTestRunner.java:69) at com.intellij.rt.junit.IdeaTestRunner$Repeater$1.execute(IdeaTestRunner.java:38) at com.intellij.rt.execution.junit.TestsRepeater.repeat(TestsRepeater.java:11) at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:35) at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:232) at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:55)
最新发布
07-14

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值