JUnit 5 中多个扩展的顺序
Order of multiple extensions in JUnit 5
如果我对 JUnit 5 使用多个扩展,顺序是什么?理想情况下,遵守 @ExtendsWith
注释的顺序,但我找不到任何相关文档。
示例:
@ExtendWith({SpringExtension.class, InitH2.class})
public class VmRepositoryIntegrationTest {
// Test implemenations
}
所以在这个例子中,我需要 Spring 来设置数据库连接,然后才能初始化数据库。
来自§5.2.1 of the JUnit 5 User Guide:
...
Multiple extensions can be registered together like this:
@ExtendWith({ DatabaseExtension.class, WebServerExtension.class })
class MyFirstTests {
// ...
}
As an alternative, multiple extensions can be registered separately like this:
@ExtendWith(DatabaseExtension.class)
@ExtendWith(WebServerExtension.class)
class MySecondTests {
// ...
}
Extension Registration Order
Extensions registered declaratively via @ExtendWith
will be executed in the order in which they are declared in the source code. For example, the execution of tests in both MyFirstTests
and MySecondTests
will be extended by the DatabaseExtension
and WebServerExtension
, in exactly that order.
如果我对 JUnit 5 使用多个扩展,顺序是什么?理想情况下,遵守 @ExtendsWith
注释的顺序,但我找不到任何相关文档。
示例:
@ExtendWith({SpringExtension.class, InitH2.class})
public class VmRepositoryIntegrationTest {
// Test implemenations
}
所以在这个例子中,我需要 Spring 来设置数据库连接,然后才能初始化数据库。
来自§5.2.1 of the JUnit 5 User Guide:
...
Multiple extensions can be registered together like this:
@ExtendWith({ DatabaseExtension.class, WebServerExtension.class }) class MyFirstTests { // ... }
As an alternative, multiple extensions can be registered separately like this:
@ExtendWith(DatabaseExtension.class) @ExtendWith(WebServerExtension.class) class MySecondTests { // ... }
Extension Registration Order
Extensions registered declaratively via
@ExtendWith
will be executed in the order in which they are declared in the source code. For example, the execution of tests in bothMyFirstTests
andMySecondTests
will be extended by theDatabaseExtension
andWebServerExtension
, in exactly that order.