JUnit 5 断言是使用测试的实际结果验证预期结果。所有JUnit Jupiter 断言都是org.junit.jupiter.Assertions类中的静态方法,方便使用。
有异常的断言将抛出一个AssertionFailedError或它的一个子类
断言方法 | 描述 |
---|---|
assertNull(java.lang.Object object) | 检查对象是否为空 |
assertNotNull(java.lang. Object object) | 检查对象是否不为空 |
assertEquals(long expected, long actual) | 检查long类型的值是否相等 |
assertEquals(double expected , double actual, double delta) | 检查指定精度的double值是否相等 |
assertFalse(boolean condition) | 检查条件是否为假 |
assertTrue(boolean condition) | 检查条件是否为真 |
assertSame(java.lang.Object expected, java.lang. Object actual) | 检查两个对象引用是否引用同一对象(即对象是否相等) |
assertNotSame(java.lang.Object unexpected, java.lang.Object actual) | 检查两个对象引用是否不引用统一对象(即对象不等) |
1、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);
}
2、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");
}
3、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);
}
4、Assertions.assertLinesMatch()示例
它断言期望的字符串列表与实际列表相匹配。 将一个字符串与另一个字符串匹配的逻辑是:
- 检查expected.equals(actual) –如果是,则继续下一对
- 否则将expected视为正则表达式,并通过String.matches(String) 检查–如果是,则继续下一对
- 否则检查expected行是否为快进标记,如果是,则相应地应用快速前行并转到 1。
有效的快进标记是以>>开头和结尾并且至少包含 4 个字符的字符串。 快进文字之间的任何字符都将被丢弃。
>>>>
>> stacktrace >>
>> single line, non Integer.parse()-able comment >>
5、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);
}
6、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);
}
7、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";
});
}
8、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;
}
9、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");
});
}
10、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)
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";
}
}
网友评论