@BeforeClass vs static {} [英] @BeforeClass vs static{}

查看:319
本文介绍了@BeforeClass vs static {}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用JUnit编写一些测试用例。我需要初始化一些静态变量,这些变量将用于该类中的所有测试用例。

I am writing some test cases using JUnit. I need to initialize some static variables which will be used for all the test cases in that class.

为此,我可以使用


  1. 静态初始化程序块或

  2. 静态方法 @BeforeClass

  1. Static initializer block or
  2. Static method with @BeforeClass

使用一个优于另一个优势有什么好处?

What are the advantages of using one over another?

推荐答案

@BeforeClass 静态初始值设定项的语义非常不同。

There are very different semantics for @BeforeClass or a static initializer.

静态初始值设定项由JVM调用,而不是由JUnit调用。如果在静态初始化程序中抛出异常,则测试框架可能无法捕获并报告异常。此外,与 @BeforeClass 方法相比,静态初始化程序的调用时间没有明确定义。它将在第一个实际 use 上运行每个类加载器一次,例如静态属性,静态方法或其构造函数之一。有时,可能很难弄清楚这是什么时候。 (如果你不使用继承:你可能有一天或某个同事会重构你的测试用例。如果不是今天,选择静态初始化器可能会在将来引入令人讨厌的错误。)

A static initializer is invoked by the JVM and not by JUnit. If an exception is thrown within a static initializer, the test framework might not be able to catch and report the exception. Furthermore, the invocation time of the static initializer is not well-defined compared to the @BeforeClass method. It will be run only once per class loader on its first actual use which is for example the access of a static property, a static method or one of its constructors. Sometimes, it might be hard to figure out when this will be. (If you do not use inheritence: You might one day or some coworker will refactor your test case. If not today, the choice for a static initializer might introduce nasty bugs in the future.)

另一方面, @BeforeClass 在每个类的测试运行之前运行。如果一个类会受到不同的测试,例如由于继承构建的测试, static 初始化程序将仅在第一次使用此类测试时运行。这意味着您使测试订单依赖于您从未想要的东西。

On the other hand, @BeforeClass is run before each class's tests are run. If a class would be subject to different tests, for example due to tests built on inheritance, the static initializer will only run for the first test using this class. This means that you made your test order dependent what is something you never want.

请注意,两个选项之间的语义差异大于使用 @Before 或测试的构造函数。作为最后的论点,考虑一下注释的纪录价值。它使您的意图更具可读性。

Note that the semantic difference between the two options is bigger than between using @Before or a constructor for a test. As a final argument, think about the documentary value of the annotations. It makes your intentions more readable.

此规则的唯一例外是不可变常量。这些应该在他们的声明中初始化,以保持你的代码简洁,并尊重编译时间常数。如果您的值是可变的,则根本不应使用 static 值。同样,在测试中改变的可变值会为您的测试引入顺序依赖性,这是应该避免的。

The only exception for this rule would be immutable constants. Those should be initialized within their declaration in order to keep your code concise and in order to respect compile time constants. If your values are however mutable, you should not use static values at all. Again, mutable values that are altered in a test introduce an order dependency to your test which is to be avoided.

TL; DR:使用 @ BeforeClass

TL;DR: Use @BeforeClass!

这篇关于@BeforeClass vs static {}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆