JUnit斷言庫提供了一組用于驗證測試結果的工具。這些斷言方法幫助開發人員在單元測試中明確表達預期結果,并在實際結果與預期結果不符時報告失敗。
1. JUnit中的斷言
斷言用于驗證測試的預期結果。JUnit 5(Jupiter)提供了一組靜態方法,可以方便地進行各種類型的斷言。
常用斷言方法
assertEquals(expected, actual)
:斷言兩個值相等。assertNotEquals(expected, actual)
:斷言兩個值不相等。assertTrue(condition)
:斷言條件為真。assertFalse(condition)
:斷言條件為假。assertNull(value)
:斷言對象為null。assertNotNull(value)
:斷言對象不為null。assertArrayEquals(expectedArray, actualArray)
:斷言兩個數組相等。assertThrows(expectedType, executable)
:斷言執行代碼塊時拋出了預期的異常。assertTimeout(duration, executable)
:斷言執行代碼塊在指定時間內完成。assertAll(executables...)
:組合多個斷言,確保所有斷言都執行并報告所有失敗。
2. 示例代碼
2.1 基本斷言示例
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;class BasicAssertionsTest {@Testvoid testEquals() {assertEquals(5, 2 + 3, "2 + 3 should equal 5");}@Testvoid testNotEquals() {assertNotEquals(5, 2 + 2, "2 + 2 should not equal 5");}@Testvoid testTrue() {assertTrue(3 > 2, "3 should be greater than 2");}@Testvoid testFalse() {assertFalse(2 > 3, "2 should not be greater than 3");}@Testvoid testNull() {Object obj = null;assertNull(obj, "Object should be null");}@Testvoid testNotNull() {Object obj = new Object();assertNotNull(obj, "Object should not be null");}
}
2.2 數組斷言示例
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;class ArrayAssertionsTest {@Testvoid testArrayEquals() {int[] expected = {1, 2, 3};int[] actual = {1, 2, 3};assertArrayEquals(expected, actual, "Arrays should be equal");}
}
2.3 異常斷言示例
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;class ExceptionAssertionsTest {@Testvoid testThrows() {assertThrows(ArithmeticException.class, () -> {int result = 1 / 0;}, "Division by zero should throw ArithmeticException");}
}
2.4 超時斷言示例
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
import java.time.Duration;class TimeoutAssertionsTest {@Testvoid testTimeout() {assertTimeout(Duration.ofMillis(100), () -> {Thread.sleep(50);}, "Execution should complete within 100 milliseconds");}
}
2.5 組合斷言示例
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;class CombinedAssertionsTest {@Testvoid testAll() {assertAll("Multiple assertions",() -> assertEquals(4, 2 * 2, "2 * 2 should equal 4"),() -> assertTrue(3 > 2, "3 should be greater than 2"),() -> assertNotNull(new Object(), "Object should not be null"));}
}
3. 自定義錯誤消息
每個斷言方法都可以接受一個可選的錯誤消息參數,當斷言失敗時會顯示該消息。這有助于快速定位問題。
示例:自定義錯誤消息
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;class CustomMessageTest {@Testvoid testWithCustomMessage() {int expected = 5;int actual = 2 + 3;assertEquals(expected, actual, "2 + 3 should equal 5, but got " + actual);}
}
4. 高級斷言技巧
4.1 組合斷言
使用assertAll
可以在一個測試方法中組合多個斷言,確保所有斷言都被執行并報告所有失敗。
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;class MultipleAssertionsTest {@Testvoid testMultipleAssertions() {assertAll("Multiple assertions",() -> assertEquals(4, 2 * 2, "2 * 2 should equal 4"),() -> assertTrue(3 > 2, "3 should be greater than 2"),() -> assertNotNull(new Object(), "Object should not be null"));}
}
4.2 條件斷言
使用assumeTrue
和assumeFalse
可以在特定條件下跳過測試。這對于依賴于特定環境或配置的測試非常有用。
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assumptions.*;class AssumptionsTest {@Testvoid testOnlyOnCiServer() {assumeTrue("CI".equals(System.getenv("ENV")), "Test only runs on CI server");// 測試代碼}@Testvoid testInAllEnvironments() {// 測試代碼assertEquals(2, 1 + 1);}
}
總結
JUnit的斷言庫提供了豐富的斷言方法,幫助開發者驗證測試結果并確保代碼的正確性。通過使用這些斷言方法,可以編寫清晰、簡潔且有效的單元測試,從而提高代碼的質量和穩定性。