断言

package com.mytest;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;

public class FirstCaseTest {

    @Test
    @DisplayName("用例1")
    void test1(){
        System.out.println("第一个测试用例");
        assertEquals(2,1+1);
        //断言为真
        assertTrue(true);
    }

    @Test
    @DisplayName("用例2")
    void test2(){
        System.out.println("第二个测试用例");
        assertNotNull(null);
    }

    @Test
    @DisplayName("用例3")
    void test3(){
        System.out.println("第三个测试用例");
        //使用assertEquals断言失败后后面的代码将不会被执行,
//assertAll分组断言可以实现所有字段的断言(第一个参数testAssert:描述信息)
assertAll("testAssert", ()->assertEquals(2,1+1), ()->assertEquals(3,1+1), ()->assertEquals(4,2+2)); } }

 

 

异常断言

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.assertThrows;


public class AssertThrowsDemo {

    void fn(int a, int b){
        System.out.println(a / b);
    }

    @Test
    void testAssertThrows() {
        // 异常断言
        assertThrows(ArithmeticException.class, () -> fn(1, 0));
    }
}

 

posted @ 2023-08-22 22:01  Mr_sven  阅读(12)  评论(0编辑  收藏  举报