對一段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類的測試結果圖:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM