E : Error 单元测试的目标是证明程序有错,而不是程序无错。为了发现代码中潜在的错误,我们需要在编写测试用例时有一些强制的错误输入(如非法数据、异常流程、非业务允许输入等)来得到预期的错误结果
二、Junit5注解
注解
用法说明
@Test
表明方法是一个测试方法
@DisplayName
为测试类或者测试方法自定义一个名称
@BeforeAll
在所有测试方法运行前运行,并且只能修饰静态方法(除非修改测试实例生命周期)
@BeforeEach
每个测试方法运行前运行
@AfterEach
每个测试方法运行完毕后运行
@AfterAll
在所有测试方法运行完毕后运行
@Disabled
这个测试不会运行
@RepeatedTest
重复测试
@TestMethodOrder
指定测试顺序排序方式
@Order
注解定义测试顺序
package com.flamingo.junit5test;import com.flamingo.junit5test.util.Add;import org.junit.jupiter.api.AfterAll;import org.junit.jupiter.api.AfterEach;import org.junit.jupiter.api.Assertions;import org.junit.jupiter.api.BeforeAll;import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.Disabled;import org.junit.jupiter.api.DisplayName;import org.junit.jupiter.api.Test;/**
* 常用注解测试
* @author flamingo
* @create 7/6/21 11:04 PM
*/@DisplayName("Common annotation test")publicclassAnnotationsTest{
privatestatic Add add;@BeforeAllpublicstaticvoidbeforeAll(){
add =newAdd();
System.out.println("Run before all test methods run");}@BeforeEachpublicvoidbeforeEach(){
System.out.println("Run before each test method runs");}@AfterEachpublicvoidafterEach(){
System.out.println("Run after each test method finishes running");}@AfterAllpublicstaticvoidafterAll(){
System.out.println("Run after all test methods have finished running");}@Disabled@Test@DisplayName("Ignore the test")publicvoiddisabledTest(){
System.out.println("This test will not run");}@Test@DisplayName("Test Methods 1+1")publicvoidtestAdd1(){
System.out.println("Running test method1+1");
Assertions.assertEquals(2, add.add(1,1));}@Test@DisplayName("Test Methods 2+2")publicvoidtestAdd2(){
System.out.println("Running test method2+2");
Assertions.assertEquals(4,add.add(2,2));}}