首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> Java 程式設計 >> Java 相關知識

JUnit 4 單元測試實戰:從入門到 TestSuite 組裝完整指南

     編輯:Java 相關知識

為什麼需要單元測試?

單元測試是保證代碼質量的基石,能在早期發現邏輯錯誤,降低後期維護成本。本文通過一個完整的 Student 實體類測試案例,演示 JUnit 4 核心用法與最佳實踐。

被測試類:Student 實體

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;
    }

    // Getter/Setter 省略...
    
    /** 年齡設置帶業務驗證:>25 返回 false */
    public boolean setAge(int age) {
        if (age > 25) return false;
        this.age = age;
        return true;
    }
}

測試類一:繼承 TestCase,使用 setUp/tearDown 生命週期

package com.phicomme.test;

import com.phicomme.hu.Student;
import junit.framework.TestCase;

public class StudentTest01 extends TestCase {
    private Student testStudent;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        testStudent = new Student("djm", "boy", 178, 24, "華東政法");
        System.out.println("setUp()");
    }

    @Override
    protected void tearDown() throws Exception {
        super.tearDown();
        System.out.println("tearDown()");
    }

    public void testGetSex() {
        assertEquals("boy", testStudent.getSex());
        System.out.println("testGetSex()");
    }

    public void testGetAge() {
        assertEquals(24, testStudent.getAge());
        System.out.println("testGetAge()");
    }
}

關鍵點:setUp() 在每個測試方法前執行,tearDown() 在每個測試方法後執行,保證測試隔離性。

測試類二:驗證業務邏輯與失敗案例

package com.phicomme.test;

import com.phicomme.hu.Student;
import junit.framework.TestCase;

public class StudentTest extends TestCase {
    private Student testStudent;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        testStudent = new Student("steven_hu", "boy", 170, 23, "上海理工");
    }

    public void testSetAge() {
        assertTrue(testStudent.setAge(21)); // 合法年齡
    }

    public void testGetSchool() {
        // 預期失敗:實際值 "上海理工" ≠ "南昌大學"
        assertEquals("南昌大學", testStudent.getSchool());
    }

    public void testGetName() {
        // 預期失敗:實際值 "steven_hu" ≠ "hdy"
        assertEquals("hdy", testStudent.getName());
    }
}

運行結果將顯示 Failure,這正是單元測試的價值——暴露代碼與預期不符。

TestSuite:組裝多測試類統一運行

package com.phicomme.test;

import junit.framework.Test;
import junit.framework.TestSuite;

public class AllTest {
    public static Test suite() {
        TestSuite suite = new TestSuite("All Tests for com.phicomme.test");
        suite.addTestSuite(StudentTest.class);
        suite.addTestSuite(StudentTest01.class);
        return suite;
    }
}

在 Eclipse 中右鍵 AllTest → Run As → JUnit Test 即可批量執行。

Eclipse 導入 JUnit 4 步驟

  1. 右鍵專案 → Properties
  2. 選 Java Build Path → Libraries → Add Library
  3. 選 JUnit → Next → 選 JUnit 4 → Finish

進階建議

  • 測試代碼與業務代碼分包管理(如 src/test/java)
  • 使用 @Before/@After 註解(JUnit 4 風格)替代繼承 TestCase
  • 引入 Mockito 進行依賴模擬,實現真正的單元隔離

掌握單元測試基礎後,可進一步探索 Android 單元測試、UI 自動化測試等進階主題。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved