先选中项目名称,单击右键,选择"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");
}
【JUnit5基本用法】@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
推荐阅读
- arduino|【arduino】DIY音乐播放器,arduino播放wav音乐,TRMpcm库测试及使用
- github|大佬深夜怒爬某 Hub 站资源,只为撸这个鉴黄平台!
- 编程语言|为什么依赖注入对于程序员来说是件好事()
- java|使用maven jacoco生成单元测试报告 并上传sonarQube
- JAVA基础|SpringBoot测试用例
- JAVA|JUnit 4教程入门