Junit5 斷言


JUnit5 斷言幫助用測試用例的實際輸出來驗證期望的輸出。為簡單起見,所有 JUnit Jupiter 斷言是org.junit.jupiter.Assertions類中的靜態方法。

Table of Contents

Assertions.assertEquals() and Assertions.assertNotEquals()
Assertions.assertArrayEquals()
Assertions.assertIterableEquals()
Assertions.assertLinesMatch()
Assertions.assertNotNull() and Assertions.assertNull()
Assertions.assertNotSame() and Assertions.assertSame()
Assertions.assertTimeout() and Assertions.assertTimeoutPreemptively()
Assertions.assertTrue() and Assertions.assertFalse()
Assertions.assertThrows()
Assertions.fail()

Assertions.assertEquals()和Assertions.assertNotEquals()示例

使用Assertions.assertEquals()斷言期望值等於實際值。 assertEquals()針對不同的數據類型(例如, int,short,float,char等。它還支持傳遞的錯誤消息,以防萬一測試失敗。 例如:

public static void assertEquals(int expected, int actual)
public static void assertEquals(int expected, int actual, String message)
public static void assertEquals(int expected, int actual, Supplier<String> messageSupplier)
void testCase() 
{
    //Test will pass
    Assertions.assertEquals(4, Calculator.add(2, 2));

    //Test will fail 
    Assertions.assertEquals(3, Calculator.add(2, 2), "Calculator.add(2, 2) test failed");

    //Test will fail 
    Supplier<String> messageSupplier  = ()-> "Calculator.add(2, 2) test failed";
    Assertions.assertEquals(3, Calculator.add(2, 2), messageSupplier);
}

類似地,Assertions.assertNotEquals()方法用於斷言期望值不等於實際值。 與assertEquals()相比,assertNotEquals()不會針對不同的數據類型重載方法,而僅接受Object

public static void assertNotEquals(Object expected, Object actual)
public static void assertNotEquals(Object expected, Object actual, String message)
public static void assertNotEquals(Object expected, Object actual, Supplier<String> messageSupplier)
void testCase() 
{
    //Test will pass
    Assertions.assertNotEquals(3, Calculator.add(2, 2));

    //Test will fail 
    Assertions.assertNotEquals(4, Calculator.add(2, 2), "Calculator.add(2, 2) test failed");

    //Test will fail 
    Supplier<String> messageSupplier  = ()-> "Calculator.add(2, 2) test failed";
    Assertions.assertNotEquals(4, Calculator.add(2, 2), messageSupplier);
}

Assertions.assertArrayEquals()示例

與assertEquals()相似,assertArrayEquals()對數組執行相同的操作,即斷言期望數組等於實際數組。它還具有針對不同數據類型的重載方法,例如boolean[],char[],int[]等。它還支持在測試失敗的情況下傳遞要打印的錯誤消息。 例如:

public static void assertArrayEquals(int[] expected, int[] actual)
public static void assertArrayEquals(int[] expected, int[] actual, String message)
public static void assertArrayEquals(int[] expected, int[] actual, Supplier<String> messageSupplier)
void testCase() 
{
    //Test will pass
    Assertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,2,3}, "Array Equal Test");

    //Test will fail because element order is different
    Assertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,3,2}, "Array Equal Test");

    //Test will fail because number of elements are different
    Assertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,2,3,4}, "Array Equal Test");
}

Assertions.assertIterableEquals()示例

它斷言期望和實際的可迭代項高度相等。 高度相等意味着集合中元素的數量和順序必須相同; 以及迭代元素必須相等

它還有 3 種重載方法。

public static void assertIterableEquals(Iterable<?> expected, Iterable> actual)
public static void assertIterableEquals(Iterable<?> expected, Iterable> actual, String message)
public static void assertIterableEquals(Iterable<?> expected, Iterable> actual, Supplier<String> messageSupplier)
@Test
void testCase() 
{
     Iterable<Integer> listOne = new ArrayList<>(Arrays.asList(1,2,3,4));
     Iterable<Integer> listTwo = new ArrayList<>(Arrays.asList(1,2,3,4));
     Iterable<Integer> listThree = new ArrayList<>(Arrays.asList(1,2,3));
     Iterable<Integer> listFour = new ArrayList<>(Arrays.asList(1,2,4,3));

    //Test will pass
    Assertions.assertIterableEquals(listOne, listTwo);

    //Test will fail
    Assertions.assertIterableEquals(listOne, listThree);

    //Test will fail
    Assertions.assertIterableEquals(listOne, listFour);
}

Assertions.assertLinesMatch()示例

斷言期望的字符串列表與實際列表相匹配。 將一個字符串與另一個字符串匹配的邏輯是

  1. 檢查expected.equals(actual) –如果是,則繼續下一對
  2. 否則將expected視為正則表達式,並通過String.matches(String) 檢查–如果是,則繼續下一對
  3. 否則檢查expected行是否為快進標記,如果是,則相應地應用快速前行並轉到 1。

有效的快進標記是以>>開頭和結尾並且至少包含 4 個字符的字符串。 快進文字之間的任何字符都將被丟棄。

>>>>
>> stacktrace >>
>> single line, non Integer.parse()-able comment >>

Assertions.assertNotNull()和Assertions.assertNull()示例

assertNotNull()斷言實際值不為空。 類似地,assertNull()方法斷言實際值為空。 兩者都有三種重載方法。

public static void assertNotNull(Object actual)
public static void assertNotNull(Object actual, String message) public static void assertNotNull(Object actual, Supplier<String> messageSupplier) public static void assertEquals(Object actual) public static void assertEquals(Object actual, String message) public static void assertEquals(Object actual, Supplier<String> messageSupplier)
@Test
void testCase() 
{     
    String nullString = null;
    String notNullString = "howtodoinjava.com";

    //Test will pass
    Assertions.assertNotNull(notNullString);

    //Test will fail
    Assertions.assertNotNull(nullString);

    //Test will pass
    Assertions.assertNull(nullString);

    // Test will fail
    Assertions.assertNull(notNullString);
}

Assertions.assertNotSame()和Assertions.assertSame()示例

assertNotSame()斷言預期和實際不引用同一對象。 同樣,assertSame()方法斷言,預期和實際引用完全相同的對象。 兩者都有三種重載方法。

public static void assertNotSame(Object actual)
public static void assertNotSame(Object actual, String message)
public static void assertNotSame(Object actual, Supplier<> messageSupplier)

public static void assertSame(Object actual)
public static void assertSame(Object actual, String message)
public static void assertSame(Object actual, Supplier<String> messageSupplier)
@Test
void testCase() 
{     
    String originalObject = "howtodoinjava.com";
    String cloneObject = originalObject;
    String otherObject = "example.com";

    //Test will pass
    Assertions.assertNotSame(originalObject, otherObject);

    //Test will fail
    Assertions.assertNotSame(originalObject, cloneObject);

    //Test will pass
    Assertions.assertSame(originalObject, cloneObject);

    // Test will fail
    Assertions.assertSame(originalObject, otherObject);
}

Assertions.assertTimeout()和Assertions.assertTimeoutPreemptively()示例

assertTimeout()和assertTimeoutPreemptively()均用於測試長時間運行的任務。 如果測試用例中的給定任務花費的時間超過指定的持續時間,則測試將失敗。

兩種方法之間唯一的區別是assertTimeoutPreemptively()中的設置,如果超過超時,Executable或ThrowingSupplier的執行將被搶先中止。 在assertTimeout()的情況下,不會中斷Executable或ThrowingSupplier。

public static void assertTimeout(Duration timeout, Executable executable)
public static void assertTimeout(Duration timeout, Executable executable, String message)
public static void assertTimeout(Duration timeout, Executable executable, Supplier<String> messageSupplier)
public static void assertTimeout(Duration timeout, ThrowingSupplier<T> supplier, String message)
public static void assertTimeout(Duration timeout, ThrowingSupplier<T> supplier, Supplier<String> messageSupplier)
@Test
void testCase() {

    //This will pass
    Assertions.assertTimeout(Duration.ofMinutes(1), () -> {
        return "result";
    });

    //This will fail
    Assertions.assertTimeout(Duration.ofMillis(100), () -> {
        Thread.sleep(200);
        return "result";
    });

    //This will fail
    Assertions.assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
        Thread.sleep(200);
        return "result";
    });
}

Assertions.assertTrue()和Assertions.assertFalse()示例

assertTrue()斷言BooleanSupplier提供的條件為真。 類似地,assertFalse()斷言提供的條件為假。 它具有以下重載方法:

public static void assertTrue(boolean condition)
public static void assertTrue(boolean condition, String message)
public static void assertTrue(boolean condition, Supplier<String> messageSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier, String message)
public static void assertTrue(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier)

public static void assertFalse(boolean condition)
public static void assertFalse(boolean condition, String message)
public static void assertFalse(boolean condition, Supplier<String> messageSupplier)
public static void assertFalse(BooleanSupplier booleanSupplier)
public static void assertFalse(BooleanSupplier booleanSupplier, String message)
public static void assertFalse(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier)
@Test
void testCase() {

    boolean trueBool = true;
    boolean falseBool = false;

    Assertions.assertTrue(trueBool);
    Assertions.assertTrue(falseBool, "test execution message");
    Assertions.assertTrue(falseBool, AppTest::message);
    Assertions.assertTrue(AppTest::getResult, AppTest::message);

    Assertions.assertFalse(falseBool);
    Assertions.assertFalse(trueBool, "test execution message");
    Assertions.assertFalse(trueBool, AppTest::message);
    Assertions.assertFalse(AppTest::getResult, AppTest::message);
}

private static String message () {
    return "Test execution result";
}

private static boolean getResult () {
    return true;
}

Assertions.assertThrows()示例

它斷言所提供的Executable的執行將引發expectedType的異常並返回該異常

 public static <T extends Throwable> T assertThrows(Class<T> expectedType, Executable executable) 

@Test
void testCase() {

    Throwable exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
        throw new IllegalArgumentException("error message");
    });
}

Assertions.fail()示例

fail()方法僅使測試失敗。 它具有以下重載方法

public static void fail(String message)
public static void fail(Throwable cause)
public static void fail(String message, Throwable cause)
public static void fail(Supplier<String> messageSupplier)

ref(32條消息) JUnit5 斷言示例_Arya_follower的博客-CSDN博客


免責聲明!

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



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