关于java:JUnit5教程3JUnit5断言

31次阅读

共计 5840 个字符,预计需要花费 15 分钟才能阅读完成。

你好啊,我是大阳。

本文咱们将探讨 JUnit5 的断言性能。

JUnit 5 断言 是应用测试的理论后果验证预期后果。所有 JUnit Jupiter 断言 都是 org.junit.jupiter.Assertions 类中的静态方法,方便使用。

有异样的断言将抛出一个 AssertionFailedError 或它的一个子类。

1 assertEquals() 和 assertNotEquals()

Assertions.assertEquals()用于断言 期望值和理论值相等 。Assertions.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)

示例:

package cn.dayangshuo.junit5.tests;
import cn.dayangshuo.junit5.Calculator;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import java.util.function.Supplier;
/**
 * @author DAYANG
 */
public class CalculatorTests {
    @Test
    @DisplayName("a + b = c")
    void addsTwoNumbers() {
        // 测试通过
        Assertions.assertEquals(4, Calculator.add(2, 2));
        // 测试失败
        Assertions.assertEquals(3, Calculator.add(2, 2), "Calculator.add(2, 2) 测试失败");
        // 测试失败
        Supplier<String> messageSupplier  = () -> "Calculator.add(2, 2) 测试失败";
        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)

示例:

@Test
@DisplayName("a + b = c")
void test() {
  // 测试通过
  Assertions.assertNotEquals(3, Calculator.add(2, 2));
  // 测试失败
  Assertions.assertNotEquals(4, Calculator.add(2, 2), "Calculator.add(2, 2) test failed");
  // 测试失败
  Supplier<String> messageSupplier = () -> "Calculator.add(2, 2) test failed";
  Assertions.assertNotEquals(4, Calculator.add(2, 2), messageSupplier);
}

2 assertArrayEquals()

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)

3 assertIterableEquals()

它断言 预期的和理论的可迭代对象是齐全相等的 齐全相等 意味着汇合中元素的数量和程序必须雷同,迭代的元素必须相等。

它还具备三个重载办法。

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)

4 assertLinesMatch()

它断言 预期的字符串列表与理论列表匹配

5 assertNotNull() 和 assertNull()

assertNotNull() 断言 理论不为空 。相同,assertNull() 办法断言理论是 null。两者都有三个重载办法:

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)

6 assertNotSame() 和 assertSame()

assertNotSame()断言 预期和理论的不援用同一个对象。 assertSame() 办法断言 预期和理论援用完全相同的对象。两者都有三个重载办法:

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

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

7 assertTimeout()

它们是用于测试长时间运行的工作,如果测试工作破费的工夫超过指定的持续时间,则测试将失败。

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() {Assertions.assertTimeout(Duration.ofMinutes(1), () -> {return "result";});
  Assertions.assertTimeout(Duration.ofMillis(100), () -> {Thread.sleep(200);
    return "result";
  });
  Assertions.assertTimeoutPreemptively(Duration.ofMillis(100), () -> {Thread.sleep(200);
    return "result";
  });
}

8 assertTrue() and assertFalse()

assertTrue()断言提供的条件为真 。相同,assertFalse() 断言提供的条件为false

两者都有以下重载办法:

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)

9 assertThrows()

assetThrows()断言提供的 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");
    });
}

10 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)
public class AppTest {
    @Test
    void testCase() {Assertions.fail("not found good reason to pass");
        Assertions.fail(AppTest::message);
    }

    private static String message () {return "not found good reason to pass";}
}

正文完
 0