@BeforeAll Method as non-static

时间:2019-04-17 02:22:15

标签: junit5

I was able to implement a non-static setup method with @BeforeAll annotation. It seems to be working correctly as only gets call once. I am bit confuse as the documentation for @BeforeAll says the method has to be static. Please explain.

@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 {...}
}

2 个答案:

答案 0 :(得分:5)

如果要使用非静态的@BeforeAll@AfterAll方法,则应change test instance lifecycleper_class

看那里:2.10. Test Instance Lifecycle

答案 1 :(得分:4)

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

@TestInstance(Lifecycle.PER_CLASS)