Junit5单元测试

引入依赖

Junit中文官方文档戳这里→Junit中文官方文档

        <!-- Junit5对应的组件-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        
       <!-- 为了兼容Junit4-->
        <dependency>
            <groupId>org.junit.vintage</groupId>
            <artifactId>junit-vintage-engine</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.hamcrest</groupId>
                    <artifactId>hamcrest-core</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

测试具体注解

DisplayName注解

@DisplayName("Junit5功能测试")
public class TestJunit5 {

    @DisplayName("DisplayName")
    @Test
    void testDisplayName(){
        System.out.println("测试testDisplayName注解");
    }
}

运行效果如下图
DisplayName效果

BeforeEach和AfterEach注解

    @BeforeEach
    void testBeforeEach(){
        System.out.println("每个测试方法执行前执行...");
    }

    @AfterEach
    void testAfterEach(){
        System.out.println("每个测试方法执行后执行...");
    }

运行效果如下图
BeforeEach和AfterEach注解效果

BeforeAll和AfterAll注解

@DisplayName("Junit5功能测试")
public class TestJunit5 {

    @DisplayName("DisplayName")
    @Test
    void testDisplayName(){
        System.out.println("测试testDisplayName注解");
    }

    @DisplayName("单纯测试案例")
    @Test
    void test(){
        System.out.println("我是单纯的测试案例");
    }

    @BeforeAll
    static void testBeforeAll(){
        System.out.println("所有测试案例开始前运行");
    }

    @AfterAll
    static void testAfterAll(){
        System.out.println("所有测试案例结束后运行");
    }
}

运行效果如下图
 BeforeAll和AfterAll注解效果

BeforeAll和AfterAll注解

    @ParameterizedTest
    @ValueSource(strings = { "racecar", "radar", "able was I ere I saw elba" })
    void palindromes(String candidate) {
        assertTrue(isPalindrome(candidate));
    }

    private boolean isPalindrome(String candidate) {
        System.out.println(candidate);
        return true;
    }

运行结果如下图
BeforeAll和AfterAll注解效果

Disabled 注解

    @Disabled  //禁用注解
    @DisplayName("测试方法2")
    @Test
    void testDisabled() {
        System.out.println("@Disabled注解禁用");
    }

运行结果如下图
Disabled 注解效果

RepeatedTest 注解

    @RepeatedTest(5) //parameter指的是重复次数
    @Test
    void testRepeatedTest() {
        System.out.println("重复执行该方法");
    }

运行结果如下图
RepeatedTest 注解效果

assertEquals 注解

    @DisplayName("standardAssertions测试")
    @Test
    void standardAssertions() {
        assertEquals(4, 4, "expected == actual 时该message内容不显示");
        assertEquals(2, 4, "expected != actual 时该message内容显示");
    }

运行结果如下图
standardAssertions 注解效果

分组+依赖断言

    @Test
    void dependentAssertions() {
        Book book = new Book(null,"Jane","story");
        // Within a code block, if an assertion fails the
        // subsequent code in the same block will be skipped.
        assertAll("properties",
                () -> {
                    String bookname = book.getBookname();
                    assertNotNull(bookname);

                    // Executed only if the previous assertion is valid.
                    assertAll("book name",
                            () -> assertTrue(bookname.startsWith("G")),
                            () -> assertTrue(bookname.endsWith("u"))
                    );
                },

                () -> {
                    String author = book.getAuthor();
                    assertNotNull(author);

                    assertAll("author",
                            () -> assertTrue(author.startsWith("J")),
                            () -> assertTrue(author.endsWith("e"))
                    );
                },

                () -> {
                    String kind = book.getKind();
                    assertNotNull(kind);

                    assertAll("kind",
                            () -> assertTrue(kind.startsWith("t"),"your expected start value is wrong"),
                            () -> assertTrue(kind.endsWith("u"),"your expected end value is wrong")
                    );
                }
        );
    }

运行结果如下图
分组+依赖断言效果

异常断言

 @Test
    void exceptionTesting() {
        Throwable exception = assertThrows(ArithmeticException.class, () -> {
            int a = 8 / 0;
            System.out.println(a);
            throw new IllegalArgumentException("a message");
        });
        assertEquals("a message", exception.getMessage());
    }

    @Test
    void timeoutNotExceeded() {
        // The following assertion succeeds.
        assertTimeout(ofMillis(2), () -> {
            // Perform task that takes less than 2 minutes.
            Thread.sleep(3);
        });
    }

运行结果如下图
异常断言效果

假设测试

    @Test
    void testOnlyOnCiServer() {
        //assertTrue不影响这句话的执行
        System.out.println("前面...");

        assertTrue(8>5);

        //assertTrue影响这句话的执行
        System.out.println("后面...");
    }

运行结果如下图
假设测试效果

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值