junit5和junit4的区别,JUnit5基本用法

 2023-09-23 阅读 27 评论 0

摘要:先选中项目名称,单击右键,选择"New" -->"JUnite Test Case"(如图1), 在“New JUnit Test Case"窗口中,填写Test case的名字,可以选择要创建的方法(如图2) 图1, 图2 package test; import sta

先选中项目名称,单击右键,选择"New" -->"JUnite Test Case"(如图1),

在“New JUnit Test Case"窗口中,填写Test case的名字,可以选择要创建的方法(如图2)

图1,

图2

package test;

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

import java.util.ArrayList;
import java.util.Collection;

import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.AfterEach;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

class JUniteTest {
    private Collection collection;

    @BeforeAll
    static void setUpBeforeClass() throws Exception {
        // one-time initialization code
        System.out.println("@BeforeClass - oneTimeSetUp");
    }

    @AfterAll
    static void tearDownAfterClass() throws Exception {
        // one-time cleanup code
        System.out.println("@AfterClass - oneTimeTearDown");
    }

    @BeforeEach
    void setUp() throws Exception {
        collection = new ArrayList();
        System.out.println("@Before - setUp");
    }

    @AfterEach
    void tearDown() throws Exception {
        collection.clear();
        System.out.println("@After - tearDown");
    }

    @Test
    public void testEmptyCollection() {
        assertTrue(collection.isEmpty());
        System.out.println("@Test - testEmptyCollection");
    }

    @Test
    public void testOneItemCollection() {
        collection.add("itemA");
        assertEquals(1, collection.size());
        System.out.println("@Test - testOneItemCollection");
    }

}

运行结果:

@BeforeClass - oneTimeSetUp
@Before - setUp
@Test - testOneItemCollection
@After - tearDown
@Before - setUp
@Test - testEmptyCollection
@After - tearDown
@AfterClass - oneTimeTearDown

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/88575.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息