[英]junit5 - is @BeforeAll necessary to instantiate objects?
Is there a difference between creating an instance variable and creating a member variable in a method marked with a @BeforeAll annotation?在标有@BeforeAll 注解的方法中创建实例变量和创建成员变量有区别吗?
Public class Example1Test{
Lift testClass = null;
@BeforeAll
public void init() {
testClass = new Lift();
}
@Test
...
}
VS VS
Public class Example2Test{
Lift testClass = new testClass;
@Test
...
}
@BoforeAll is used to make sure that the annotated method should be executed before all the @ParameterizedTest,@Test, @RepeatedTest and @TestFactory methods in the current class. @BoforeAll 用于确保被注释的方法应该在当前类中的所有@ParameterizedTest、@Test、@RepeatedTest 和@TestFactory 方法之前执行。 so if you want this method to be run before any other method then yes you have to annotate this.因此,如果您希望此方法在任何其他方法之前运行,那么是的,您必须对此进行注释。
If object, that is defined and initialized at the same time (like Lift class in your code snippet), has state, it's better to initialize it in @BeforeEach
.如果同时定义和初始化的对象(例如代码片段中的 Lift 类)具有状态,则最好在@BeforeEach
中对其进行初始化。 Object's context is cleared before every test.每次测试前清除对象的上下文。 This makes your test independent from each other.这使您的测试彼此独立。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.