java單元測試(Junit)

來源:互聯網
上載者:User

標籤:

最基本的模組測試

1:首先建立一個java工程,在工程中建立一個被單元測試的Student資料類,如下:

 

package UnitTest;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;    }}
Student

 

2:在eclipse下單元測試這個類:  

 

首先匯入Junit包:選 中java工程,點擊滑鼠右鍵--->選擇properties---->在視窗中選Java Build Path---->在右側點擊Add Library---->在彈出的視窗列表中選中Junit---->下一步----->Junit 4(我用的是Junit 4)---->finish

這樣Junit 4包就導完了,接下來就是建立測試類別:

 

將測試類別和被測試類別放在不同的包中(也可以放在同一個包中,此處只是為了區別),代碼如下:

 

測試類別1:

 

package UnitTestExample;import UnitTest.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("李鵬", "boy", 172, 21, "華東交大");        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(21, testStudent.getAge());        System.out.println("testGetAge()");    }}
StudentTest01

 

測試類別2:

 

package UnitTestExample;import UnitTest.Student;import junit.framework.TestCase;public class StudentTest02 extends TestCase {    private Student testStudent;    @Override    protected void setUp() throws Exception {        // TODO Auto-generated method stub        super.setUp();        testStudent = new Student("小明", "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("小花", testStudent.getName());    }}
StudentTest02

 

當然,如果同時需要一起測試以上這兩個測試類別,可以通過TestSuite類實現,它相當於是一個套件,可以把所有測試類別添進來一起運行測試;

 

綜合測試類別:

 

package UnitTestExample;import UnitTest.Student;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(StudentTest02.class);        suite.addTestSuite(StudentTest01.class);        return suite;    }}
AllTest

 

 最後,分別測試以上三個類(選中需要測試的類---->滑鼠右鍵---->Run As---->Junit Test):

StudentTest01類測試結果:

StudentTest02類測試結果:

AllTest類測試結果:

 

java單元測試(Junit)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.