【问题标题】:@BeforeAll Method as non-static@BeforeAll 方法为非静态
【发布时间】:2019-09-07 04:42:59
【问题描述】:

我能够使用@BeforeAll 注释实现非静态设置方法。 它似乎工作正常,因为只被调用一次。 我有点困惑,因为@BeforeAll 的文档说该方法必须是静态的。请解释一下。

@TestMethodOrder(OrderAnnotation.class)
@SpringJUnitWebConfig(locations = { "classpath:service.xml" }) 
@TestInstance(Lifecycle.PER_CLASS)
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Documented 
@Inherited 
public class MyTest
{
    @BeforeAll
    public void setup() throws Exception {...}
}

【问题讨论】:

  • 请出示您的完整代码。
  • 如果给定的答案没有阐明你的观点,你应该展示完整的代码,其中至少包括一个带有导入和注释的完整测试类。为什么?因为任何事情都可能有所作为。
  • 我已经添加了我正在使用的注释。谢谢。
  • 他们确实揭示了发生了什么:-)

标签: junit5


【解决方案1】:

如果你想使用非静态的@BeforeAll@AfterAll 方法,你应该change test instance lifecycleper_class

看那里:2.10. Test Instance Lifecycle

【讨论】:

  • 我使用的是@TestInstance(Lifecycle.PER_CLASS)。现在我知道这就是它允许我运行非静态 @beforeAll 方法的原因。谢谢
【解决方案2】:

您只需使用下面的 sn-p 注释您的测试类(包含 @BeforeAll 方法),您就可以开始了。

@TestInstance(Lifecycle.PER_CLASS)

【讨论】:

  • 另一个选项是:添加文件 junit-platform.properties 并写入:junit.jupiter.testinstance.lifecycle.default = per_class
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-14
  • 1970-01-01
相关资源
最近更新 更多