Have plugin automatically configure Unit (JavaTestFramework not found)
Hi,
Summary: I am trying to get my plugin to set up JUnit for the user (if it is not set up). However, the code that does this is not exposed in the API..
Background: I am writing a plug-in that interacts with JUnit. The plug-in will be used be novice users, so if JUnit is not set up, the plug-in attempts to set it up for them. My solution is based on code in com.intellij.testIntegration.createTest.CreateTestDialog.java.
Approach: I find the appropriate framework descriptor:
TestFramework descriptor = null;
List<TestFramework> descriptors = new ArrayList<>(TestFramework.EXTENSION_NAME.getExtensionList());
for (TestFramework framework : descriptors) {
if (framework.getName().equals("JUnit5")
&& framework.getClass().getName().startsWith("com.intellij.execution.junit")) {
descriptor = framework;
break;
}
}
I then check if the library is already attached:
if (descriptor.isLibraryAttached(module)) {
return;
}
Otherwise, I need to setup the library.
In CreateTestDialog, this is done by narrowing the type of descriptor to JavvTestFreamwork and calling setupLibrary() (in line 411 of createCentralPanel()
((JavaTestFramework)mySelectedFramework).setupLibrary(myTargetModule)
But, JavaTestFramework does not appear to be included in the API.
The Hack: I use reflection to get around this:
try {
Method setupLibrary = descriptor.getClass().getMethod("setupLibrary", Module.class);
setupLibrary.invoke(descriptor, modules[0]);
} ...
This is not “the right way” to do this, as I am relying on a hidden API.
Question: Is there a recommended way of doing this?
Please sign in to leave a comment.
Quick Addendum: Is this the better way?
Hi,
I don't understand how
MavenDependencyUtil.addFromMaven(…);
would help here.Regarding
JavaTestFramework
, please make sure your plugin has a dependency on the Java (com.intellij.java
) plugin. See:https://plugins.jetbrains.com/docs/intellij/plugin-dependencies.html
Hi Karol,
Thanks! I only realized 60 minutes ago that MavenDependencyUtil is only used to add libraries when running tests. So, I withdraw that thought. I also realized I needed to reload the plugin.xml and build.gradle.kts to make the IDE recognize the JavaTestFramework classes. Problem solved. :). Thank you for your prompt response.