主要的区别是
@Before和@BeforeClass @BeforeEach和@BeforeAll
@After和@AfterClass根据JUnit Api,@Before用于以下情况:
在编写测试时,通常会发现有几个测试需要创建类似的对象才能运行。
而@BeforeClass可用于建立数据库连接。但是@Before不能做同样的事情吗?
发布于 2013-11-30 01:50:15
标记为@Before的代码在每个测试之前执行,而@BeforeClass在整个测试夹具之前运行一次。如果您的测试类有十个测试,那么@Before代码将被执行十次,但是@BeforeClass只执行一次。
通常,当多个测试需要共享相同计算开销的设置代码时,使用@BeforeClass。建立数据库连接属于此类别。您可以将代码从@BeforeClass迁移到@Before,但是测试运行可能需要更长的时间。注意,标记为@BeforeClass的代码是作为静态初始化程序运行的,因此它将在创建测试夹具的类实例之前运行。
在JUnit 5中,标记@BeforeEach和@BeforeAll相当于JUnit 4中的@Before和@BeforeClass。它们的名称更多地表示它们何时运行,松散地解释为:“在每个测试之前”和“在所有测试之前一次”。
发布于 2017-07-11 14:49:25
每个注释之间的差异是:
+-------------------------------------------------------------------------------------------------------+
¦ 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. ¦ ¦ ¦
+-------------------------------------------------------------------------------------------------------+两个版本中的大多数注释都是相同的,但很少有不同之处。
执行顺序.
虚线框->可选注释。

发布于 2018-08-13 11:05:38
and与BeforeClass in JUnit
函数@Before注释将在具有@Test注释的类中的每个测试函数之前执行,但是带有@BeforeClass的函数将在类中的所有测试函数之前执行一次。
类似地,带有@After注释的函数将在具有@Test注释的类中的每个测试函数之后执行,但在类中的所有测试函数之后,带有@AfterClass的函数只执行一次。
SampleClass
public class SampleClass {
public String initializeData(){
return "Initialize";
}
public String processDate(){
return "Process";
}
}SampleTest
public class SampleTest {
private SampleClass sampleClass;
@BeforeClass
public static void beforeClassFunction(){
System.out.println("Before Class");
}
@Before
public void beforeFunction(){
sampleClass=new SampleClass();
System.out.println("Before Function");
}
@After
public void afterFunction(){
System.out.println("After Function");
}
@AfterClass
public static void afterClassFunction(){
System.out.println("After Class");
}
@Test
public void initializeTest(){
Assert.assertEquals("Initailization check", "Initialize", sampleClass.initializeData() );
}
@Test
public 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 = @AfterAllhttps://stackoverflow.com/questions/20295578
复制相似问题