简体   繁体   English

junit5 - @BeforeAll 是实例化对象所必需的吗?

[英]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.

相关问题 IntellIj 不显示来自@BeforeAll JUnit5 的异常 - IntellIj not displaying exceptions from @BeforeAll JUnit5 使用@BeforeAll 在 JUnit 中实例化一个 object 返回 null - Using @BeforeAll to instantiate an object in JUnit returns null Junit5 永远不会在 @Suite class 内击中 @BeforeAll - Junit5 never hitting @BeforeAll inside @Suite class 将 Spring @Value 传递给静态 @BeforeAll Junit5 方法 - Pass Spring @Value into static @BeforeAll Junit5 method Junit5 和 Maven:@BeforeAll 初始化方法未被调用 - Junit5 and Maven: @BeforeAll initialization method not being called Junit5 参数化测试生命周期。 为什么 @BeforeAll 和 static 块在所有测试之后运行 - Junit5 Parameterized Test LifeCycle. why @BeforeAll and static block run after all the tests mvn test + JUnit5 + log4j:为什么在@BeforeAll 和@BeforeEach 中打印的日志不是 output 到控制台? - mvn test + JUnit5 + log4j: Why logs printed in @BeforeAll and @BeforeEach are not output to console? 对于每个嵌套的测试类,都会执行 junit5 扩展的 BeforeAll / AfterAll 回调。 这是预期的吗? - BeforeAll / AfterAll callbacks of junit5 extension are executed for each nested test class. Is this expected? 如何在 Junit5 中检查嵌套对象是否相等 - How to check the nested objects are equal or not in Junit5 jUnit @beforeAll不起作用 - jUnit @beforeAll not working
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM