我在JUnit5中编写了一个动态测试 @TestFactory
,现在我've seen I can' t做 @Before
用于动态测试,指的是JUnit 5 User Guide - Writing Test Annotations .
是否有任何解决方法可以执行 @Before
或类似于TestFactory之前的方法?
方法我想放入 @Before
,因为它只是初始化的东西:
public static void initialize() throws Exception{
buildTest = new XQueryTestHelper();
buildTest.initialization();
listTestSuiteIdentifier = buildTest.getListTestsuiteIdentifier();
arrayHdrInbPayTestcases = buildTest.getHdrInbPayTestcases();
arrayHeaderAndBodyTestcases = buildTest.getHeaderAndBodyTestcases();
listHeaderAndBodyTestSuites = buildTest.getHeaderAndBodyTestSuites();
listHdrInbPayTestSuites = buildTest.getHdrInbPayTestsuites();
}
Solution:
在动态测试中可以使用@BeforeAll / @AfterAll . 参考Improve documentation of DynamicTest lifecycle
动态测试生命周期动态测试的执行生命周期与标准@Test案例的执行生命周期完全不同 . 具体来说,动态测试没有任何生命周期回调 . 这意味着不会为动态测试执行@BeforeEach和@AfterEach方法及其相应的扩展回调 . 换句话说,如果从lambda表达式中的测试实例访问字段以进行动态测试,那么这些字段将不会被执行由相同@TestFactory方法生成的动态测试之间的回调方法或扩展重置 .
1 回答
在动态测试中可以使用
@BeforeAll / @AfterAll
. 参考Improve documentation of DynamicTest lifecycle