blue_man
blue_man

Reputation: 63

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

I am setting some properties as below when I launch the Junit Tests programmatically.

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

Is there a way to access My_Param from the test method?

Upvotes: 1

Views: 1593

Answers (1)

Arho Huttunen
Arho Huttunen

Reputation: 1131

I believe you can use the ExtensionContext.getConfigurationParameter() method.

From JUnit 5.1.0 release notes:

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

The obvious way to access the ExtensionContext would be to implement an extension.

An alternative would be to implement one of the lifecycle callbacks directly in the test:

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

Upvotes: 2

Related Questions