你好啊,我是大阳。
本文我们将讨论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 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 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 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 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 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 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 messageSupplier)public static void assertEquals(Object actual)
public static void assertEquals(Object actual, String message)
public static void assertEquals(Object actual, Supplier 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 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 messageSupplier)
public static void assertTimeout(Duration timeout, ThrowingSupplier supplier, String message)
public static void assertTimeout(Duration timeout, ThrowingSupplier supplier, Supplier 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 messageSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier)
public static void assertTrue(BooleanSupplier booleanSupplier, String message)
public static void assertTrue(BooleanSupplier booleanSupplier, Supplier messageSupplier)public static void assertFalse(boolean condition)
public static void assertFalse(boolean condition, String message)
public static void assertFalse(boolean condition, Supplier messageSupplier)
public static void assertFalse(BooleanSupplier booleanSupplier)
public static void assertFalse(BooleanSupplier booleanSupplier, String message)
public static void assertFalse(BooleanSupplier booleanSupplier, Supplier messageSupplier)
9 assertThrows() assetThrows()断言提供的
Executable
执行会引发expectedType
异常并返回异常//重载方法
public static T assertThrows(Class expectedType, Executable executable)
//示例
@Test
void testCase() {
Throwable exception = Assertions.assertThrows(IllegalArgumentException.class, () -> {
throw new IllegalArgumentException("error message");
});
}
10 fail() 【JUnit5教程(3):_JUnit5断言】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 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";
}
}
推荐阅读
- 全网最硬核 Java 新内存模型解析与实验 - 2. 原子访问与字分裂
- 全网最硬核 Java 新内存模型解析与实验单篇版(不断更新QA中)
- JNI(本地代码调用Java代码)
- 局部变量表中的起始PC
- 京东一面(说说 CompletableFuture 的实现原理和使用场景(我懵了。。))
- 程序人生|面试字节跳动,我被怼了。
- java|码神之路博客项目部署
- 笔记|纯手写SpringFramework-第二代(原创)
- Java|云原生DevOps(k8s/docker/springcloudalibaba)