@BeforeAll方法为非静态

Mee*_*era 0 junit5

我能够实现带有@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 {...}
}
Run Code Online (Sandbox Code Playgroud)

Wil*_*son 20

您只需要使用@BeforeAll下面的代码片段注释您的测试类(包含方法),您就可以开始了。

@TestInstance(Lifecycle.PER_CLASS)
Run Code Online (Sandbox Code Playgroud)


Ser*_*gey 7

如果要使用非静态@BeforeAll@AfterAll方法,则应将测试实例生命周期更改per_class

看那里:2.10。测试实例生命周期

  • 我正在使用@TestInstance(Lifecycle.PER_CLASS)。现在我知道这就是它允许我运行非静态 @beforeAll 方法的原因。谢谢 (2认同)