1

I am writing an extension for a test using @ParameterizedTest. My test looks like:

@ParameterizedTest
@ValueSource(strings={"string1", "string2"})
public void test1(String name){
    System.out.println(name);
}

In the extensions, how do I get the TestDesciptor to find out which invocation in currently active?

public class MyExtension implements BeforeEachCallBack{
   
 @Override
 public void beforeEach(ExtensionContext extensionContext)   
     //  What to do here? extensionContext.?
 }
}
Progman
  • 16,827
  • 6
  • 33
  • 48

1 Answers1

2

As you can see by its package name org.junit.platform.engine.TestDescriptor is not part of JUnit Jupiter API which has org.junit.jupiter.api as its base package. Moreover, org.junit.platform.engine is not even imported by the API.

So the sad answer is you cannot get at the test descriptor, at least not without some dirty and unstable reflection. I am, however, quite sure that the underlying reason you have for wanting access can be mitigated in a different way. So what is it you’d like to achieve?

johanneslink
  • 4,877
  • 1
  • 20
  • 37
  • Thank you for your reply. The situation is: I have 2 tests I need to implement. Each one has a unique id in Jira (Xray) - TestID1 and TestID2. Both can be implemented in the same test method using @ParameterizedTest However, I need to be aware which is currently running so I will be able to pair it with the correct Jira ticket. This information is provided in the TestDescriptor object.... Any ideas? – nir gallner Dec 09 '21 at 14:45
  • You may try a TestExecutionListener: https://junit.org/junit5/docs/current/user-guide/#launcher-api-listeners-custom. With it you have access to TestIdentifier and thereby the unique ID. – johanneslink Dec 10 '21 at 06:40
  • This is the better link: https://junit.org/junit5/docs/current/user-guide/#launcher-api-listeners-custom – johanneslink Dec 10 '21 at 06:47