测试——Junit

内容大纲:

常用的五个注解

测试用例顺序指定

参数化

测试套件

断言

1. 常用的五个注解

1.1 @Test

通常情况下,我们输入要写在main方法下,此时我想直接输出:

java 复制代码
@Test
void Test01(){
    System.out.println("================第一个测试用例");
}

1.2 @BeforeAll @AfterAll

BeforeALL在Test修饰的方法之前运行,AfterAll在之后运行

java 复制代码
    @Test
    void Test01(){
        System.out.println("================第一个测试用例");
    }

    @Test
    void Test02(){
        System.out.println("================第二个测试用例");
    }

    @BeforeAll
    static void Start(){
        System.out.println("=================开始测试===============");
    }

    @AfterAll
    static void End(){
        System.out.println("=================结束测试===============");
    }

1.3 @BeforeEach @AfterEach

java 复制代码
    @Test
    void Test01(){
        System.out.println("================第一个测试用例");
    }

    @Test
    void Test02(){
        System.out.println("================第二个测试用例");
    }

    @BeforeEach
    void StartV1(){
        System.out.println("***开始执行***");}

    @AfterEach
    void EndV1(){
        System.out.println("***结束执行***");
    }

2. 测试用例顺序指定

复制代码
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)
复制代码
@Order(2)
java 复制代码
@TestMethodOrder(MethodOrderer.OrderAnnotation.class)

    @Order(2)
    @Test
    void Test01(){
        System.out.println("================第一个测试用例");
    }

    @Order(1)
    @Test
    void Test02(){
        System.out.println("================第二个测试用例");
    }

3. 参数化

1.1 单参数

java 复制代码
    /**
     * 单参数
     */
    @ParameterizedTest
    @ValueSource(ints={1,2,3,4})
    void Test03(int x){
        System.out.println("接收的参数x:" + x);
    }

单参数有他的弊端,只能接收一种类型的参数,对应的Test方法只能接收一个参数

1.2 多参数

java 复制代码
    /**
     * 多参数
     */
    @ParameterizedTest
    @CsvSource({"'张三','28'"})
    void Test04(String name,int age){
        System.out.println(name + "今年" + age +"岁");
    }

读取文件中的数据进行操作:

每组数据用逗号隔开

java 复制代码
    //多参数,读取文件中的数据,进行操作
    @ParameterizedTest
    @CsvFileSource(resources = "test01.csv")
    void Test05(String name,int age){
        System.out.println("name: " + name + ",age: "+age);
    }

注意:

参数化对应的注解上如果写了@Test,测试用例就会执行两次,其中一次就会报错,因为Test和方法混合,没有找到参数,所以报错

1.3 通过方法生成参数

java 复制代码
    /**
     * 通过方法生成参数
     */
    @ParameterizedTest
    @MethodSource("Generate")
    void Test06(String name,int age){
        System.out.println("name: " + name + ",age: "+age);
    }


    public static Stream<Arguments> Generate() {
        return Stream.of(
                Arguments.arguments("张山",12),
                Arguments.arguments("lisi",23));
    }

4. 测试套件

@Suite

4.1 通过class运行测试用例

java 复制代码
@Suite
//通过class运行测序用例
@SelectClasses({JunitTest2.class,JunitTest.class})
public class RunTests {
}

4.2 通过包运行测试用例

java 复制代码
@Suite
//通过包运行测试用例
@SelectPackages(value = {"example"})
public class RunTests {
}

5. 断言

断言相等/不相等/为空/不为空

java 复制代码
    /**
     * 断言
     */
    @Test
    void Test07(){
        int x = 10;
        int y = 20;
        Assertions.assertEquals(x,y);//判断断言是否相等
        Assertions.assertNotEquals(x,y);//判断断言是否不相等
        String temp = null;
        Assertions.assertNull(temp);//判断断言是否为空
        Assertions.assertNotNull(temp);//判断断言是否不为空
    }

6. 依赖

java 复制代码
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-api -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-api</artifactId>
            <version>5.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-params -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-params</artifactId>
            <version>5.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.platform/junit-platform-suite -->
        <dependency>
            <groupId>org.junit.platform</groupId>
            <artifactId>junit-platform-suite</artifactId>
            <version>1.9.1</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.junit.jupiter/junit-jupiter-engine -->
        <dependency>
            <groupId>org.junit.jupiter</groupId>
            <artifactId>junit-jupiter-engine</artifactId>
            <version>5.9.1</version>
            <scope>test</scope>
        </dependency>
相关推荐
喜欢打篮球的普通人8 小时前
LLVM后端指令选择
android·java·javascript
某不知名網友8 小时前
C++ 七大排序算法完整讲解
java·算法·排序算法
CodeStats8 小时前
《源纹天书》第一百六十一章至第一百六十五章:开源化宣言、元定义的力量、栈帧重构、容器道场升级、技术债的化身!
java·源纹天书
不良手残8 小时前
MyBatisPlus代码自动生成器
java
SamDeepThinking9 小时前
一次线程池线上故障复盘:四层防线如何避免数据丢失
java·后端·程序员
花生智源9 小时前
Java实现Prompt工程的技巧——从模板到工程化,告别字符串拼接
java·人工智能
折哥的程序人生 · 物流技术专研9 小时前
Java 23 种设计模式:从踩坑到精通 | 番外:策略 vs 模板方法 —— 组合与继承的终极对决
java·策略模式·java面试·comparator·模版方法模式·java设计模式·从踩坑到精通
库克克9 小时前
【C++】类和对象--this指针详解
java·开发语言·c++
SL_staff9 小时前
JVS低代码完整源码解析:如何二次开发自己的表单组件?
java·低代码·vuex
QN1幻化引擎9 小时前
Gravity-Anchored Cognitive Field Architecture: The DalinX V8/V10 Implementation
java·前端·算法