首页 > 代码库 > 对一段Java代码的单元测试

对一段Java代码的单元测试

  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---->finish

 

测试类1 2

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, "XX大学");          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()");      }  }  

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, "XX大学");      }        @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("XX大学", testStudent.getSchool());      }            public void testGetName()      {          assertEquals("hdy", testStudent.getName());      }  }  

 

StudentTest类的测试结果图:

技术分享

 

StudentTest01类的测试结果图:

技术分享

对一段Java代码的单元测试