@BeforeAll 方法作为非静态

@BeforeAll Method as non-static

我能够使用 @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 {...}
}

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

看那里:2.10. Test Instance Lifecycle

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

@TestInstance(Lifecycle.PER_CLASS)