原文: https://howtodoinjava.com/junit5/junit-5-assertions-examples/

JUnit5 断言帮助用测试用例的实际输出来验证期望的输出。 为简单起见,所有 JUnit Jupiter 断言org.junit.jupiter.Assertions类中的静态方法。

  1. Table of Contents
  2. Assertions.assertEquals() and Assertions.assertNotEquals()
  3. Assertions.assertArrayEquals()
  4. Assertions.assertIterableEquals()
  5. Assertions.assertLinesMatch()
  6. Assertions.assertNotNull() and Assertions.assertNull()
  7. Assertions.assertNotSame() and Assertions.assertSame()
  8. Assertions.assertTimeout() and Assertions.assertTimeoutPreemptively()
  9. Assertions.assertTrue() and Assertions.assertFalse()
  10. Assertions.assertThrows()
  11. Assertions.fail()

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

使用Assertions.assertEquals()断言期望值等于实际值assertEquals()针对不同的数据类型(例如, intshortfloatchar等。它还支持传递传递的错误消息,以防万一测试失败。 例如:

  1. public static void assertEquals(int expected, int actual)
  2. public static void assertEquals(int expected, int actual, String message)
  3. public static void assertEquals(int expected, int actual, Supplier<String< messageSupplier)
  1. void testCase()
  2. {
  3. //Test will pass
  4. Assertions.assertEquals(4, Calculator.add(2, 2));
  5. //Test will fail
  6. Assertions.assertEquals(3, Calculator.add(2, 2), "Calculator.add(2, 2) test failed");
  7. //Test will fail
  8. Supplier&lt;String&gt; messageSupplier = ()-> "Calculator.add(2, 2) test failed";
  9. Assertions.assertEquals(3, Calculator.add(2, 2), messageSupplier);
  10. }

类似地,Assertions.assertNotEquals()方法用于断言期望值不等于实际值。 与assertEquals()相比,assertNotEquals()不会针对不同的数据类型重载方法,而仅接受Object

  1. public static void assertNotEquals(Object expected, Object actual)
  2. public static void assertNotEquals(Object expected, Object actual, String message)
  3. public static void assertNotEquals(Object expected, Object actual, Supplier<String> messageSupplier)
  1. void testCase()
  2. {
  3. //Test will pass
  4. Assertions.assertNotEquals(3, Calculator.add(2, 2));
  5. //Test will fail
  6. Assertions.assertNotEquals(4, Calculator.add(2, 2), "Calculator.add(2, 2) test failed");
  7. //Test will fail
  8. Supplier&lt;String&gt; messageSupplier = ()-> "Calculator.add(2, 2) test failed";
  9. Assertions.assertNotEquals(4, Calculator.add(2, 2), messageSupplier);
  10. }

Assertions.assertArrayEquals()示例

assertEquals()相似,assertArrayEquals()对数组执行相同的操作,即断言期望数组等于实际数组。 它还具有针对不同数据类型的重载方法,例如boolean[]char[]int[]等。它还支持在测试失败的情况下传递要打印的错误消息。 例如:

  1. public static void assertArrayEquals(int[] expected, int[] actual)
  2. public static void assertArrayEquals(int[] expected, int[] actual, String message)
  3. public static void assertArrayEquals(int[] expected, int[] actual, Supplier<String> messageSupplier)
  1. void testCase()
  2. {
  3. //Test will pass
  4. Assertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,2,3}, "Array Equal Test");
  5. //Test will fail because element order is different
  6. Assertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,3,2}, "Array Equal Test");
  7. //Test will fail because number of elements are different
  8. Assertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,2,3,4}, "Array Equal Test");
  9. }

Assertions.assertIterableEquals()示例

它断言期望和实际的可迭代项高度相等。 高度相等意味着集合中元素的数量和顺序必须相同; 以及迭代元素必须相等。

它还有 3 种重载方法。

  1. public static void assertIterableEquals(Iterable<?> expected, Iterable> actual)
  2. public static void assertIterableEquals(Iterable<?> expected, Iterable> actual, String message)
  3. public static void assertIterableEquals(Iterable<?> expected, Iterable> actual, Supplier<String> messageSupplier)
  1. @Test
  2. void testCase()
  3. {
  4. Iterable<Integer> listOne = new ArrayList<>(Arrays.asList(1,2,3,4));
  5. Iterable<Integer> listTwo = new ArrayList<>(Arrays.asList(1,2,3,4));
  6. Iterable<Integer> listThree = new ArrayList<>(Arrays.asList(1,2,3));
  7. Iterable<Integer> listFour = new ArrayList<>(Arrays.asList(1,2,4,3));
  8. //Test will pass
  9. Assertions.assertIterableEquals(listOne, listTwo);
  10. //Test will fail
  11. Assertions.assertIterableEquals(listOne, listThree);
  12. //Test will fail
  13. Assertions.assertIterableEquals(listOne, listFour);
  14. }

Assertions.assertLinesMatch()示例

它断言期望的字符串列表与实际列表相匹配。 将一个字符串与另一个字符串匹配的逻辑是:

  1. 检查expected.equals(actual) –如果是,则继续下一对
  2. 否则将expected视为正则表达式,并通过
    String.matches(String) 检查–如果是,则继续下一对
  3. 否则检查expected行是否为快进标记,如果是,则相应地应用
    快速前行并转到 1。

有效的快进标记是以>>开头和结尾并且至少包含 4 个字符的字符串。 快进文字之间的任何字符都将被丢弃。

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

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

assertNotNull()断言实际值不为空。 类似地,assertNull()方法断言实际值为空。 两者都有三种重载方法。

  1. public static void assertNotNull(Object actual)
  2. public static void assertNotNull(Object actual, String message)
  3. public static void assertNotNull(Object actual, Supplier<String> messageSupplier)
  4. public static void assertEquals(Object actual)
  5. public static void assertEquals(Object actual, String message)
  6. public static void assertEquals(Object actual, Supplier<String> messageSupplier)
  1. @Test
  2. void testCase()
  3. {
  4. String nullString = null;
  5. String notNullString = "howtodoinjava.com";
  6. //Test will pass
  7. Assertions.assertNotNull(notNullString);
  8. //Test will fail
  9. Assertions.assertNotNull(nullString);
  10. //Test will pass
  11. Assertions.assertNull(nullString);
  12. // Test will fail
  13. Assertions.assertNull(notNullString);
  14. }

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

assertNotSame()断言预期和实际不引用同一对象。 同样,assertSame()方法断言,预期和实际引用完全相同的对象。 两者都有三种重载方法。

  1. public static void assertNotSame(Object actual)
  2. public static void assertNotSame(Object actual, String message)
  3. public static void assertNotSame(Object actual, Supplier<> messageSupplier)
  4. public static void assertSame(Object actual)
  5. public static void assertSame(Object actual, String message)
  6. public static void assertSame(Object actual, Supplier<String> messageSupplier)
  1. @Test
  2. void testCase()
  3. {
  4. String originalObject = "howtodoinjava.com";
  5. String cloneObject = originalObject;
  6. String otherObject = "example.com";
  7. //Test will pass
  8. Assertions.assertNotSame(originalObject, otherObject);
  9. //Test will fail
  10. Assertions.assertNotSame(originalObject, cloneObject);
  11. //Test will pass
  12. Assertions.assertSame(originalObject, cloneObject);
  13. // Test will fail
  14. Assertions.assertSame(originalObject, otherObject);
  15. }

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

assertTimeout()assertTimeoutPreemptively()均用于测试长时间运行的任务。 如果测试用例中的给定任务花费的时间超过指定的持续时间,则测试将失败。

两种方法之间唯一的区别是assertTimeoutPreemptively()中的设置,如果超过超时,ExecutableThrowingSupplier的执行将被抢先中止。 在assertTimeout()的情况下,不会中断ExecutableThrowingSupplier

  1. public static void assertTimeout(Duration timeout, Executable executable)
  2. public static void assertTimeout(Duration timeout, Executable executable, String message)
  3. public static void assertTimeout(Duration timeout, Executable executable, Supplier<String> messageSupplier)
  4. public static void assertTimeout(Duration timeout, ThrowingSupplier<T> supplier, String message)
  5. public static void assertTimeout(Duration timeout, ThrowingSupplier<T> supplier, Supplier<String> messageSupplier)
  1. @Test
  2. void testCase() {
  3. //This will pass
  4. Assertions.assertTimeout(Duration.ofMinutes(1), () -> {
  5. return "result";
  6. });
  7. //This will fail
  8. Assertions.assertTimeout(Duration.ofMillis(100), () -> {
  9. Thread.sleep(200);
  10. return "result";
  11. });
  12. //This will fail
  13. Assertions.assertTimeoutPreemptively(Duration.ofMillis(100), () -> {
  14. Thread.sleep(200);
  15. return "result";
  16. });
  17. }

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

assertTrue()断言BooleanSupplier提供的条件为真。 类似地,assertFalse()断言提供的条件为假。 它具有以下重载方法:

  1. public static void assertTrue(boolean condition)
  2. public static void assertTrue(boolean condition, String message)
  3. public static void assertTrue(boolean condition, Supplier<String> messageSupplier)
  4. public static void assertTrue(BooleanSupplier booleanSupplier)
  5. public static void assertTrue(BooleanSupplier booleanSupplier, String message)
  6. public static void assertTrue(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier)
  7. public static void assertFalse(boolean condition)
  8. public static void assertFalse(boolean condition, String message)
  9. public static void assertFalse(boolean condition, Supplier<String> messageSupplier)
  10. public static void assertFalse(BooleanSupplier booleanSupplier)
  11. public static void assertFalse(BooleanSupplier booleanSupplier, String message)
  12. public static void assertFalse(BooleanSupplier booleanSupplier, Supplier<String> messageSupplier)
  1. @Test
  2. void testCase() {
  3. boolean trueBool = true;
  4. boolean falseBool = false;
  5. Assertions.assertTrue(trueBool);
  6. Assertions.assertTrue(falseBool, "test execution message");
  7. Assertions.assertTrue(falseBool, AppTest::message);
  8. Assertions.assertTrue(AppTest::getResult, AppTest::message);
  9. Assertions.assertFalse(falseBool);
  10. Assertions.assertFalse(trueBool, "test execution message");
  11. Assertions.assertFalse(trueBool, AppTest::message);
  12. Assertions.assertFalse(AppTest::getResult, AppTest::message);
  13. }
  14. private static String message () {
  15. return "Test execution result";
  16. }
  17. private static boolean getResult () {
  18. return true;
  19. }

Assertions.assertThrows()示例

它断言所提供的Executable的执行将引发expectedType的异常并返回该异常。

  1. public static <T extends Throwable> T assertThrows(Class<T> expectedType,
  2. Executable executable)
  1. @Test
  2. void testCase() {
  3. Throwable exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
  4. throw new IllegalArgumentException("error message");
  5. });
  6. }

Assertions.fail()示例

fail()方法仅使测试失败。 它具有以下重载方法:

  1. public static void fail(String message)
  2. public static void fail(Throwable cause)
  3. public static void fail(String message, Throwable cause)
  4. public static void fail(Supplier<String> messageSupplier)
  1. public class AppTest {
  2. @Test
  3. void testCase() {
  4. Assertions.fail("not found good reason to pass");
  5. Assertions.fail(AppTest::message);
  6. }
  7. private static String message () {
  8. return "not found good reason to pass";
  9. }
  10. }

将我的问题放在评论部分。

学习愉快!