Junit 5 extension - how to access testDescriptor

836 views Asked by At

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.?
 }
}
1

There are 1 answers

3
johanneslink On

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?