Reputation: 93
Is there a way to access the JUnit5 version during runtime?
E.g.
...
System.out.printf( "JUnit: %s\n", junit.runner.Version.id() );
...
worked fine for JUnit4.
I am looking for the "counterpart" for JUnit5
THANKS :-)
Upvotes: 1
Views: 756
Reputation: 93
Thanks for your answer, but I am still having problems. E.g. I can not access ".getVersion()"
Anyway
...
import java.lang.module.ModuleDescriptor;
import java.lang.module.ModuleDescriptor.Version;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.junit.platform.runner.JUnitPlatform;
...
String getJupiterVersionFromModuleDescriptor(){
final Class<Test> clazz = org.junit.jupiter.api.Test.class;
final Module module = clazz.getModule();
final ModuleDescriptor moduleDescriptor = module.getDescriptor();
final Optional<Version> optionalVersion = moduleDescriptor.version();
final Version version = optionalVersion.get();
return version.toString();
}
String String getPlatformVersionFromModuleDescriptor(){
final Class<JUnitPlatform> clazz = org.junit.platform.runner.JUnitPlatform.class;
final Module module = clazz.getModule();
final ModuleDescriptor moduleDescriptor = module.getDescriptor();
final Optional<Version> optionalVersion = moduleDescriptor.version();
final Version version = optionalVersion.get();
return version.toString();
}
String getJupiterVersionFromManifest(){
final Class<Test> clazz = org.junit.jupiter.api.Test.class;
final Package pakage = clazz.getPackage();
final String version = pakage.getImplementationVersion();
return version;
}
String getPlatformVersionFromManifest(){
final Class<JUnitPlatform> clazz = org.junit.platform.runner.JUnitPlatform.class;
final Package pakage = clazz.getPackage();
final String version = pakage.getImplementationVersion();
return version;
}// delivers null in my case - getPlatformVersionFromModuleDescriptor() reports 1.5.1 on my machine
works and of course:
org.junit.jupiter.api.Test.class.getModule().getDescriptor().version()
org.junit.platform.runner.JUnitPlatform.class.getModule().getDescriptor().version()
org.junit.jupiter.api.Test.class.getPackage().getImplementationVersion()
org.junit.platform.runner.JUnitPlatform.class.getPackage().getImplementationVersion()
works also.
Currently I am using:
String getJupiterVersion(){
final Class<Test> testClass = org.junit.jupiter.api.Test.class;
final Optional<Version> optionalVersion = testClass.getModule().getDescriptor().version();
return optionalVersion.isPresent() ? optionalVersion.get().toString() : testClass.getPackage().getImplementationVersion();
}
String getPlatformVersion(){
final Class<JUnitPlatform> jUnitPlatformClass = org.junit.platform.runner.JUnitPlatform.class;
final Optional<Version> optionalVersion = jUnitPlatformClass.getModule().getDescriptor().version();
return optionalVersion.isPresent() ? optionalVersion.get().toString() : jUnitPlatformClass.getPackage().getImplementationVersion();
}
Further: Get jar version in runtime might be helpful.
Anyway, I am still having problems to access the vintage version. E.g. org.junit.vintage.engine.VintageTestEngine is not accessible ???
And just recently on some machine running JUnit 5.4 getDescriptor() resp. e.g. org.junit.jupiter.api.Test.class.getModule().getDescriptor() delivered null. I like to have a way of identifying the JUnit version that runs on any JUnit 5 installation.
Upvotes: 0
Reputation: 9089
There is no single "JUnit 5" version number.
There are three of them. They are listed on junit.org:
All artifacts of these three groups contain version information packaged
a) into their module descriptor and
b) into their manifest files.
For Jupiter for example, you may use
a) org.junit.jupiter.api.Test.class.getModule().getDescriptor().getVersion()
or b) org.junit.jupiter.api.Test.class.getPackage().getImplementationVersion()
to access Jupiter's version information at runtime.
Upvotes: 3