服务器之家

服务器之家 > 正文

java编程之单元测试(Junit)实例分析(附实例源码)

时间:2020-01-20 11:39     来源/作者:stevenhu_223

本文实例讲述了java编程单元测试。分享给大家供大家参考,具体如下:

完整实例代码代码点击此处本站下载

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

废话少说,上代码:

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

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

在eclipse下单元测试这个类:

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

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

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

测试类1:

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

测试类2:

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

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

代码如下:

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

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

StudentTest类的测试结果图:

java编程之单元测试(Junit)实例分析(附实例源码)

StudentTest01类的测试结果图:

java编程之单元测试(Junit)实例分析(附实例源码)

AllTest类的测试结果图:

java编程之单元测试(Junit)实例分析(附实例源码)

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

希望本文所述对大家Java程序设计有所帮助。

相关文章

热门资讯

玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分
玄元剑仙肉身有什么用 玄元剑仙肉身境界等级划分 2019-06-21
男生常说24816是什么意思?女生说13579是什么意思?
男生常说24816是什么意思?女生说13579是什么意思? 2019-09-17
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情
华为nova5pro和p30pro哪个好 华为nova5pro和华为p30pro对比详情 2019-06-22
配置IIS网站web服务器的安全策略配置解决方案
配置IIS网站web服务器的安全策略配置解决方案 2019-05-23
Nginx服务器究竟是怎么执行PHP项目
Nginx服务器究竟是怎么执行PHP项目 2019-05-24
返回顶部

363
Weibo Article 1 Weibo Article 2 Weibo Article 3 Weibo Article 4 Weibo Article 5 Weibo Article 6 Weibo Article 7 Weibo Article 8 Weibo Article 9 Weibo Article 10 Weibo Article 11 Weibo Article 12 Weibo Article 13 Weibo Article 14 Weibo Article 15 Weibo Article 16 Weibo Article 17 Weibo Article 18 Weibo Article 19 Weibo Article 20 Weibo Article 21 Weibo Article 22 Weibo Article 23 Weibo Article 24 Weibo Article 25 Weibo Article 26 Weibo Article 27 Weibo Article 28 Weibo Article 29 Weibo Article 30 Weibo Article 31 Weibo Article 32 Weibo Article 33 Weibo Article 34 Weibo Article 35 Weibo Article 36 Weibo Article 37 Weibo Article 38 Weibo Article 39 Weibo Article 40