zoukankan      html  css  js  c++  java
  • 使用JUnit4进行java单元测试

     第一步:创建一个java工程,在工程中创建一个被单元测试的Student数据类,代码如下:

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

    部分截图如下:

     图 1-1

    第二步:在eclipse下单元测试这个类
        首先,导入Junit包:选中java工程,点击鼠标右键--->选择属性---->在窗口中选Java构建路径---->选择库--->右侧点击添加库---->在弹出的窗口列表中选中Junit---->下一步----->Junit 4---->完成--->确定

    部分截图如下:

     图 2-1

    这样Junit 4包就导完了,接下来就是创建测试类:选中java工程,点击鼠标右键--->选择新建--->JUnit测试用例--->按图2-2编辑--->完成

     图 2-2

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

     1 package com.junittest.yu.test;
     2 
     3 import com.junittest.yu.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-3

    按以上方法创建测试类(2)代码如下:

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

    完成截图如下:

     图 2-3

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

    代码如下:

     1 package com.junittest.yu.test;
     2 
     3 
     4 
     5 //import com.junittest.yu.test.StudentTest;
     6 import com.junittest.yu.test.StudentTest01;
     7 
     8 import junit.framework.Test;
     9 import junit.framework.TestSuite;
    10 
    11 public class AllTest
    12 {
    13 
    14     //static PersonTest p = new PersonTest();
    15     //static PersonTest p1 = new PersonTest();
    16     public static Test suite()
    17     {
    18         TestSuite suite = new TestSuite("Test for com.junittest.yu.test.");
    19         //suite.addTest(p);
    20         //suite.addTest(p1);
    21         suite.addTestSuite(StudentTest.class);
    22         suite.addTestSuite(StudentTest01.class);
    23         return suite;
    24         
    25     }
    26 }

    完成截图如下:

    图 2-4  

    第三步:分别测试以上三个类(选中需要测试的类---->鼠标右键---->运行方式---->Junit 测试)
    StudentTest01类测试结果截图如下:

    图 3-1

    StudentTest类测试结果截图如下:

    图 3-2

    AllTest类测试结果截图如下:

    图 3-3

    到此java类的单元测试就完成了。

    第四步:将Eclipse中java代码上传到GitHub

    图 4-1

    在java项目上右击--->Team--->共享项目--->Git--->按图4-2操作--->完成

    图 4-2

    将代码上传到GitHub:

    在java项目上右击--->Team--->commit--->选中所有文件--->commit --->见图4-3--->URI就是GitHub自己仓库的地址--->见图4-4--->见图4-5--->图4-6

    图 4-3

    图 4-4

    图 4-5

     

    图 4-6

    至此上传完成。

    完成各阶段所需时间:

    1,被测类代码编写 30%

    2,测试类代码编写 40%

    3,运行测试 10%

    4,上传代码到GitHub 20%

    附:GitHub  JUnit_Test库地址 https://github.com/yujk1230/JUnit_Test


     

    参考资料:http://my.oschina.net/Chaos777/blog/349167

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

           

     

     

    ————来自yujk1230
  • 相关阅读:
    求100内的数和
    汉诺塔(印度传说)
    ORA-06502:PL/SQL:数字或值错误:数值精度太高
    触发器的编写
    mysql改密码
    LeetCode-238 Product of Array Except Self
    LeetCode-236 Lowest Common Ancestor of a Binary Tree
    LeetCode-233 Number of Digit One
    LeetCode-230 Kth Smallest Element in a BST
    LeetCode-229 Majority Element II
  • 原文地址:https://www.cnblogs.com/yujk1230/p/4784317.html
Copyright © 2011-2022 走看看