有没有办法从测试中访问 Junit 配置参数

Is there a way to access the Junit configuration parameter from the test

当我以编程方式启动 Junit 测试时,我正在设置一些属性,如下所示。

    LauncherDiscoveryRequestBuilder
            .request()
            .selectors(selectMethod("com.example#testMethod()"))
            .configurationParameter("My_Param","Hello")
            .build()

有没有办法从测试方法访问My_Param

我相信你可以使用ExtensionContext.getConfigurationParameter()方法。

来自 JUnit 5.1.0 发行说明:

Extensions for JUnit Jupiter can now access JUnit Platform configuration parameters at runtime via the new getConfigurationParameter(String key) method in the ExtensionContext API.

访问 ExtensionContext 的明显方法是实现扩展。

另一种方法是直接在测试中实现生命周期回调之一:

public class YourTest implements BeforeEachCallback {
  @Override
  public void beforeEach(ExtensionContext context) throws Exception {
  }
}