@Before,@BeforeClass,@BeforeEach和@BeforeAll之间的区别
@Before
的代码在每次测试之前执行
@BeforeClass
在整个测试方法执行之前运行一次
如果您的测试类有十个测试,则@Before
代码将执行十次,但是@BeforeClass
将仅执行一次。
当多个测试需要共享相同的代码时,可以使用@BeforeClass
。 建立数据库连接属于此类。
您可以将代码从@BeforeClass
移到@Before
,但是您的测试运行可能需要更长的时间。
注意,标记为@BeforeClass
的代码作为静态初始化程序运行,因此它将在创建测试夹具的类实例之前运行。
在JUnit 5中 ,标签@BeforeEach
和@BeforeAll
与JUnit 4中的@Before
和@BeforeClass
等效。
它们的名称更能说明它们的运行时间,可以解释为:“在所有测试之前执行一次”和“每次测试之前执行”。
Junit4和Junit5中每个注释之间的区别:
+-------------------------------------------------------------------------------------------------------+
¦ Feature ¦ Junit 4 ¦ Junit 5 ¦
¦--------------------------------------------------------------------------+--------------+-------------¦
¦ Execute before all test methods of the class are executed. ¦ @BeforeClass ¦ @BeforeAll ¦
¦ Used with static method. ¦ ¦ ¦
¦ For example, This method could contain some initialization code ¦ ¦ ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute after all test methods in the current class. ¦ @AfterClass ¦ @AfterAll ¦
¦ Used with static method. ¦ ¦ ¦
¦ For example, This method could contain some cleanup code. ¦ ¦ ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute before each test method. ¦ @Before ¦ @BeforeEach ¦
¦ Used with non-static method. ¦ ¦ ¦
¦ For example, to reinitialize some class attributes used by the methods. ¦ ¦ ¦
¦-------------------------------------------------------------------------------------------------------¦
¦ Execute after each test method. ¦ @After ¦ @AfterEach ¦
¦ Used with non-static method. ¦ ¦ ¦
¦ For example, to roll back database modifications. ¦ ¦ ¦
+-------------------------------------------------------------------------------------------------------+
两个版本中的大多数注释都相同,但几乎没有区别
执行顺序
@Before
注释函数将在具有@Test
注释的类中的每个测试函数之前执行
@BeforeClass
注释函数仅在类中的所有测试函数之前执行一次
@After
注释
函数将在类中具有@Test
批注的每个测试函数之后执行
@AfterClass
注释函数仅在该类中的所有测试函数之后执行一次
类
public class SampleClass {public String initializeData(){return "Initialize";}public String processDate(){return "Process";}}
测试
public class SampleTest {private SampleClass sampleClass;@BeforeClasspublic static void beforeClassFunction(){System.out.println("Before Class");}@Beforepublic void beforeFunction(){sampleClass=new SampleClass();System.out.println("Before Function");}@Afterpublic void afterFunction(){System.out.println("After Function");}@AfterClasspublic static void afterClassFunction(){System.out.println("After Class");}@Testpublic void initializeTest(){Assert.assertEquals("Initailization check", "Initialize", sampleClass.initializeData() );}@Testpublic void processTest(){Assert.assertEquals("Process check", "Process", sampleClass.processDate() );}}
输出
Before Class
Before Function
After Function
Before Function
After Function
After Class
Junit 5中
@Before = @BeforeEach
@BeforeClass = @BeforeAll
@After = @AfterEach
@AfterClass = @AfterAll
import org.junit.Assert
import org.junit.Before
import org.junit.BeforeClass
import org.junit.Testclass FeatureTest {companion object {private lateinit var heavyFeature: HeavyFeature@BeforeClass@JvmStaticfun beforeHeavy() {heavyFeature = HeavyFeature()}}private lateinit var feature: Feature@Beforefun before() {feature = Feature()}@Testfun testCool() {Assert.assertTrue(heavyFeature.cool())Assert.assertTrue(feature.cool())}@Testfun testWow() {Assert.assertTrue(heavyFeature.wow())Assert.assertTrue(feature.wow())}
}
如同
import org.junit.Assert
import org.junit.Testclass FeatureTest {companion object {private val heavyFeature = HeavyFeature()}private val feature = Feature()@Testfun testCool() {Assert.assertTrue(heavyFeature.cool())Assert.assertTrue(feature.cool())}@Testfun testWow() {Assert.assertTrue(heavyFeature.wow())Assert.assertTrue(feature.wow())}
}
@Before,@BeforeClass,@BeforeEach和@BeforeAll之间的区别相关推荐
- @ Before,@ BeforeClass,@ BeforeEach和@BeforeAll之间的区别
本文翻译自:Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll What is the main differen ...
- @Before, @BeforeClass, @BeforeEach 和 @BeforeAll之间的不同
1. 不同注解的区别如下: 特性 Junit 4 Junit 5 在当前类的所有测试方法之前执行. 注解在静态方法上. 此方法可以包含一些初始化代码. @BeforeClass @BeforeAll ...
- FPGA与ASIC:它们之间的区别以及使用哪一种?
FPGA与ASIC:它们之间的区别以及使用哪一种? FPGA Vs ASIC: Differences Between Them And Which One To Use? VL82C486 Sing ...
- Python 应用领域以及版本之间的区别
Python 应用领域以及版本之间的区别 一.Python应用领域 Python+人工智能,给你更多研究方向选择! 企业级综合实战项目,集六大前沿技术为一体 二. Python 2与Python 3的 ...
- java中separator_java - File.separator和路径中的斜杠之间的区别
java - File.separator和路径中的斜杠之间的区别 在Java Path-String中使用/和普通的File.separator有什么区别? 与双反斜杠相比,/平台独立似乎不是原因, ...
- 机器学习、数据科学、人工智能、深度学习和统计学之间的区别!
↑↑↑关注后"星标"Datawhale 每日干货 & 每月组队学习,不错过 Datawhale干货 作者:Vincent Granville,来源:机器之心 在这篇文章中, ...
- Python里面None True False之间的区别
None虽然跟True False一样都是布尔值. 虽然None不表示任何数据,但却具有很重要的作用. 它和False之间的区别还是很大的! 例子: >>> t = None > ...
- 2.javascript之缓存 localStorage 和sessionStorage之间的区别
2018-08-04 前言 今天做项目的时候遇到了这个问题,用户登录成功之后如何改变将登录的链接切换为用户名 解决方案:使用了sessionstorage缓存 和js的onload加载事件 用户登录成 ...
- java se 与j2se_关于java:J2EE和J2SE项目之间的区别
本问题已经有最佳答案,请猛点这里访问. 我已经从github下载了一个开源项目. 这是一个基于Maven的项目. 我如何理解该项目是J2SE项目还是J2EE项目? 这两种项目在结构上有何不同? Jav ...
最新文章
- 利用动态加载模板,配合ajax实现无刷新操作
- 进击webpack 4 (基础篇一)
- 减少科研文章写作错误的一点小技巧
- javascript获取网页URL地址及参数等
- C#时间格式转换、日期操作函数、常用转换语法
- c/c++面试试题(三)
- 【JVM】通过javap命令分析Java汇编指令
- 公司邮箱发邮件的util类
- 使用Ecliplse时,对导入package的顺序进行设定
- Tensflow的equal函数
- WCF与 WebService的区别
- 谷歌浏览器插件离线安装
- 遗补:“预防‘磁碟机’病毒”
- 【01】分别使用头插法和尾插法建立单链表
- Esp8266闪存文件系统LIttleFS
- 2021-12-23 714. 买卖股票的时机含手续费(动态规划)
- 写一个PE的壳_Part 5:PE格式修复+lief源码修改
- WPF DataGrid 获取选中 一行 或者 多行
- 30多岁零基础想转行学编程,来得及吗?
- jmeter导入自定义jar包