一个Java小白的学习之路 个人博客 youngljx.top
文章目录
JUnit4 与 JUnit 5 对比
常用注解
JUnit 4将所有内容捆绑到单个jar文件中,Junit 4需要Java 5或更高版本。
Junit 5由3个子项目组成
即JUnit Platform,JUnit Jupiter和JUnit Vintage,Junit 5需要Java 8或更高版本。
- JUnit平台 它定义了TestEngine用于开发在平台上运行的新测试框架的API。
- JUnit Jupiter
- 它具有所有新的junit注释和TestEngine实现,以运行使用这些注释编写的测试。
- JUnit Jupiter 支持在JUnit 5平台上运行JUnit 3和JUnit 4编写的测试。
断言org.junit.Assert类
Asser类中主要方法如下:
在Junit 4中,org.junit.Assert具有所有断言方法来验证预期结果和结果。
它们接受错误消息的额外参数作为方法签名中的FIRST参数。例如:
public static void assertEquals(long expected, long actual)
public static void assertEquals(String message, long expected, long actual)
在JUnit 5中,org.junit.jupiter.Assertions包含大多数断言方法,包括附加assertThrows()和assertAll()方法。assertAll()到目前为止处于实验状态,并用于分组断言。
JUnit 5断言方法也有重载方法,以支持在测试失败时传递错误消息,例如:
public static void assertEquals(long expected, long actual)
public static void assertEquals(long expected, long actual, String message)
public static void assertEquals(long expected, long actual, Supplier messageSupplier)
断言assertThat使用
假设 Assume类
在Junit 4中,org.junit.Assume包含用于说明测试有意义的条件的假设的方法。它有以下五种方法:
assumeFalse()
assumeNoException()
assumeNotNull()
假使,假设()
assumeTrue()
在Junit 5中,org.junit.jupiter.api.Assumptions包含用于说明测试有意义的条件的假设的方法。它有以下三种方法:
assumeFalse()
假使,假设()
assumeTrue()
打包测试
在JUnit 4,@RunWith和@Suite注释。例如:
import org.junit.runner.RunWith;
import org.junit.runners.Suite;