@ Before,@ BeforeClass,@ BeforeEach和@BeforeAll之间的区别
本文翻译自:Difference between @Before, @BeforeClass, @BeforeEach and @BeforeAll
What is the main difference between 之间的主要区别是什么
@Before
and@BeforeClass
@Before
和@BeforeClass
- and in JUnit 5
@BeforeEach
and@BeforeAll
在JUnit 5中@BeforeEach
和@BeforeAll
- and in JUnit 5
@After
and@AfterClass
@After
和@AfterClass
According to the JUnit Api @Before
is used in the following case: 根据JUnit Api ,在以下情况下使用@Before
:
When writing tests, it is common to find that several tests need similar objects created before they can run. 编写测试时,通常会发现几个测试需要先创建类似的对象,然后才能运行。
Whereas @BeforeClass
can be used to establish a database connection. 而@BeforeClass
可用于建立数据库连接。 But couldn't @Before
do the same? 但是@Before
不能一样吗?
#1楼
参考:https://stackoom.com/question/1N9o2/Before-BeforeClass-BeforeEach和-BeforeAll之间的区别
#2楼
The code marked @Before
is executed before each test, while @BeforeClass
runs once before the entire test fixture. 标有@Before
的代码在每次测试之前执行,而@BeforeClass
在整个测试夹具之前运行一次。 If your test class has ten tests, @Before
code will be executed ten times, but @BeforeClass
will be executed only once. 如果您的测试类有十个测试,则@Before
代码将执行十次,但是@BeforeClass
将仅执行一次。
In general, you use @BeforeClass
when multiple tests need to share the same computationally expensive setup code. 通常,当多个测试需要共享相同的计算昂贵的设置代码时,可以使用@BeforeClass
。 Establishing a database connection falls into this category. 建立数据库连接属于此类。 You can move code from @BeforeClass
into @Before
, but your test run may take longer. 您可以将代码从@BeforeClass
移到@Before
,但是您的测试运行可能需要更长的时间。 Note that the code marked @BeforeClass
is run as static initializer, therefore it will run before the class instance of your test fixture is created. 注意,标记为@BeforeClass
的代码作为静态初始化程序运行,因此它将在创建测试夹具的类实例之前运行。
In JUnit 5 , the tags @BeforeEach
and @BeforeAll
are the equivalents of @Before
and @BeforeClass
in JUnit 4. Their names are a bit more indicative of when they run, loosely interpreted: 'before each tests' and 'once before all tests'. 在JUnit 5中 ,标签@BeforeEach
和@BeforeAll
与JUnit 4中的@Before
和@BeforeClass
等效。它们的名称更能指示它们的运行时间,松散地解释为:“每次测试之前”和“一次在所有测试之前”。 '。
#3楼
Difference between each annotation are : 每个注释之间的区别是:
+-------------------------------------------------------------------------------------------------------+
¦ 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. ¦ ¦ ¦
+-------------------------------------------------------------------------------------------------------+
Most of annotations in both versions are same, but few differs. 两个版本中的大多数注释都相同,但几乎没有区别。
Reference 参考
Order of Execution. 执行顺序。
Dashed box -> optional annotation. 虚线框->可选注释。
#4楼
Before and BeforeClass in JUnit JUnit中的BeforeClass和BeforeClass
The function @Before
annotation will be executed before each of test function in the class having @Test
annotation but the function with @BeforeClass
will be execute only one time before all the test functions in the class. @Before
注释函数将在具有@Test
注释的类中的每个测试函数之前执行,而具有@BeforeClass
的函数仅在类中的所有测试函数之前执行一次。
Similarly function with @After
annotation will be executed after each of test function in the class having @Test
annotation but the function with @AfterClass
will be execute only one time after all the test functions in the class. 类似地,具有@After
批注的函数将在类中具有@Test
批注的每个测试函数之后执行,但是具有@AfterClass
的函数仅在该类中的所有测试函数之后执行一次。
SampleClass 样本类
public class SampleClass {public String initializeData(){return "Initialize";}public String processDate(){return "Process";}}
SampleTest 样品测试
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() );}}
Output 输出量
Before Class
Before Function
After Function
Before Function
After Function
After Class
In Junit 5 在Junit 5中
@Before = @BeforeEach
@BeforeClass = @BeforeAll
@After = @AfterEach
@AfterClass = @AfterAll
#5楼
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())}
}
Same as 如同
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之间的区别
@Before的代码在每次测试之前执行 @BeforeClass在整个测试方法执行之前运行一次 如果您的测试类有十个测试,则@Before代码将执行十次,但是@BeforeClass将仅执行一次. 当 ...
- @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 ...
最新文章
- python argv 详解_Python3 sys.argv[ ]用法详解
- oracle获取父级,如何通过sql获取oracle connect中的最终父id列
- JavaScript越来越简单啦啦啦
- 中国大学MOOC 计算机组成原理第5章 测试(下)
- entity framework学习笔记
- 数据库事务特征、数据库隔离级别,以及各级别数据库加锁情况(含实操)--read uncommitted篇...
- apache 配置用户级目录
- “威金(Worm.Viking)”病毒特点-专杀及_desktop.ini删除
- 如何解决计算机前置音频设备无声音输出的方法之一
- stm32连接热敏打印机
- 华为手机如何安装Goole play教程及安装包
- [SAP ABAP开发技术总结]SD销售订单定价过程
- WFP之关联上下文数据以及注意事项
- 一文让你知道测试职业到底有哪些发展方向
- 【霍罗维兹数据结构】线索二叉树 | THREADED BINARY TREES
- navigationBar 标题字体颜色设置
- 教你 IntelliJ IDEA 永久激活,建议收藏!(转)
- Python解析html获取超链接地址并下载解析
- 日记--javascriptApache Echarts
- 【沉淀】从网络中间件到搜索,从移动开发到分布式计算平台,阿里高级专家李睿博谈自己的折腾路...