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

Table of ContentsAssertions.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 passAssertions.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 passAssertions.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 passAssertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,2,3}, "Array Equal Test");//Test will fail because element order is differentAssertions.assertArrayEquals(new int[]{1,2,3}, new int[]{1,3,2}, "Array Equal Test");//Test will fail because number of elements are differentAssertions.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 passAssertions.assertIterableEquals(listOne, listTwo);//Test will failAssertions.assertIterableEquals(listOne, listThree);//Test will failAssertions.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 passAssertions.assertNotNull(notNullString);//Test will failAssertions.assertNotNull(nullString);//Test will passAssertions.assertNull(nullString);// Test will failAssertions.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 passAssertions.assertNotSame(originalObject, otherObject);//Test will failAssertions.assertNotSame(originalObject, cloneObject);//Test will passAssertions.assertSame(originalObject, cloneObject);// Test will failAssertions.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 passAssertions.assertTimeout(Duration.ofMinutes(1), () -> {return "result";});//This will failAssertions.assertTimeout(Duration.ofMillis(100), () -> {Thread.sleep(200);return "result";});//This will failAssertions.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)
public class AppTest {@Testvoid testCase() {Assertions.fail("not found good reason to pass");Assertions.fail(AppTest::message);}private static String message () {return "not found good reason to pass";}
}

JUnit5 断言示例相关推荐

  1. 【SpringBoot2—junit5断言、前置条件】

    文章目录 一.断言(assertions) 1. 简单断言方法 2. 数组断言 3. 组合断言 4. 异常断言 5. 超时断言 6.快速失败 二.前置条件(assumptions) 一.断言(asse ...

  2. Junit5使用示例

    一.创建单元测试service 右键service->new->other->Junit Test Case->next 选择需要创建单元测试的方法或者直接点击finish 文 ...

  3. postman断言示例:Response body:Is equal to a string

    Response body:Is equal to a string:检查response body等于指定字符串 进行接口测试时,添加断言时必不可少的,断言就是判断响应内容与预期返回是否一致 通过T ...

  4. JUnit5 假设示例

    JUnit5 Assumptions类提供静态方法来支持基于假设的条件测试执行. 假设失败会导致测试中止. 假设通常在继续执行给定测试方法没有意义的情况下使用. 在测试报告中,这些测试将被标记为通过. ...

  5. postman断言示例:Response body:Contains string

    Response body:Contains string:检查响应主体是否包含字符串 下面以一个接口实例来说下如何处理断言 pm.test("Body matches string&quo ...

  6. postman断言示例:Response body:JSON value check

    Response body:JSON value check:检查Response body中JSON某个字段所对应的值 下面以一个接口实例来说下如何处理断言 上图是接口的Response body ...

  7. JUnit5 预期的异常 – assertThrows()示例

    在 JUnit5 中,要测试异常情况,则应使用org.junit.jupiter.api.Assertions.assertThrows()方法. JUnit5 异常测试还有其他方法,但我建议避免使用 ...

  8. JUnit5 @Tag注解示例

    JUnit5 @Tag 可用于从测试计划中过滤测试用例. 它可以帮助针对不同的环境,不同的用例或任何特定要求创建多个不同的测试计划.您可以通过仅在测试计划中包括那些标记的测试或通过从测试计划中排除其他 ...

  9. SpringBoot教程(14) JUnit5详解 断言 assertEquals assertSame assertThrows assertThat

    一.前言 断言,简单理解就是用来判断的语句.判断待测试的代码的结果和我们期望的结果是否一致.如果不一致,则说明这个UT失败了. 我们最最常见的断言就是assertEquals,判断值是否相等.JUni ...

最新文章

  1. PHP eval函数
  2. WebService大讲堂之Axis2(8):异步调用WebService
  3. C++::CPLEX文件读写
  4. 解决.gitgnore加入.idea无效问题
  5. LiveVideoStack Meet成都 生活与技术的“矛盾体”(内附资料下载)
  6. Spring5参考指南:基于Schema的AOP
  7. 【Android开发】NDK开发(3)-jni开发技巧
  8. IB COM Read
  9. 计算机网络系统由什么系统组成,从资源构成上看计算机网络系统由什么构成
  10. 一步一步安装服务器监视软件MRTG
  11. 防火墙访问控制Access Control
  12. Android 从零开始实现微信支付
  13. python函数中的变量取出来_在Python中从函数调用中提取变量
  14. 安卓中自定义view控件代替radiogroup实现颜色渐变效果的写法
  15. jsp页面适应手机屏幕_Jsp编写的页面如何适应手机浏览器页面
  16. a DNS-1123 label must consist of lower case alphanumeric characters or ‘-‘, and must start and end w
  17. 常见问题汇总:FLUENT文件导出为CAS或MSH
  18. 2.4g和5g要不要合并_路由器WiFi 2.4G和5G要不要合并?双频合一的缺点分析
  19. WPS作图常见问题+LATLEX
  20. Windows优化大师之驱动智能备份

热门文章

  1. PostgreSQL学习总结(6)—— PostgreSQL 模式(SCHEMA)详解
  2. 产品经理学习总结(1)——人人都是产品经理之需求文档语法
  3. Linux学习总结(19)——Linux中文本编辑器vim特殊使用方法
  4. Spring MVC学习总结(12)——Spring MVC集成Swagger时报错{schemaValidationMessages:[
  5. 基于ForkJoin构建一个简单易用的并发组件
  6. Apprenda发布Kubernetes商业版,PaaS、CaaS任君选择
  7. iOS开发-16进制颜色转换
  8. 解决只可以上QQ却不可以上网问题
  9. 数据库访问接口的代码
  10. 《暖冬里的事儿 ——春假散札》