@Before、@BeforeClass、@BeforeEach 和@BeforeAll 之间的区别

新手上路,请多包涵

之间的主要区别是什么

  • @Before@BeforeClass
    • 在 JUnit 5 @BeforeEach@BeforeAll
  • @After@AfterClass

根据 JUnit Api @Before 用于以下情况:

在编写测试时,通常会发现多个测试需要在运行前创建相似的对象。

@BeforeClass 可用于建立数据库连接。但是不能 @Before 做同样的事情吗?

原文由 Evgenij Reznik 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 1.4k
2 个回答

标记为 @Before 的代码在每次测试之前执行,而 @BeforeClass 在整个测试夹具之前运行一次。如果你的测试类有十个测试, @Before 代码将被执行十次,但是 @BeforeClass 将只执行一次。

通常,当多个测试需要共享相同的计算量大的设置代码时,您可以使用 @BeforeClass 。建立数据库连接属于这一类。您可以将代码从 @BeforeClass 移动到 @Before ,但您的测试运行可能需要更长的时间。请注意,标记为 @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指示它们何时运行,松散地解释为:“在每次测试之前”和“在所有测试之前一次”。

原文由 Sergey Kalinichenko 发布,翻译遵循 CC BY-SA 3.0 许可协议

每个注释之间的区别是:

 +-------------------------------------------------------------------------------------------------------+
¦                                       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.                        ¦              ¦             ¦
+-------------------------------------------------------------------------------------------------------+

两个版本中的大部分注释相同,但很少有不同。

参考

执行顺序。

虚线框 -> 可选注释。

在此处输入图像描述

原文由 Joby Wilson Mathews 发布,翻译遵循 CC BY-SA 3.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题