5

I have a database that stores my test results. I'm interested in writing a plugin for intellij13 that will let me rerun the test failures from the database using the JUnit run configuration. I can't find any documentation on this.

I'd like to see an example for some method like:

public void runTest(String testClass, String testName) {...}

kukido
  • 10,431
  • 1
  • 45
  • 52
munk
  • 12,340
  • 8
  • 51
  • 71
  • [How do I run JUnit tests from inside my java application?](http://stackoverflow.com/questions/2543912/how-do-i-run-junit-tests-from-inside-my-java-application) – kukido Jan 26 '14 at 02:58
  • @kukido My question isn't about running JUnit tests programmatically in general. I'd like to take advantage of the run action intellij already provides. – munk Jan 26 '14 at 03:21
  • I apologize, bad question comprehension. Last time I looked into available option in 12.x, and there were no public classes available to create JUnit run configuration. I did not check 13.x, maybe it became more open. I suggest to get source code for Community Edition and look for JUnit run configuration. Please update the question if you find something interesting. – kukido Jan 26 '14 at 03:30
  • Added answer with the solution. – kukido Jan 29 '14 at 16:40

1 Answers1

5

I looked into IntelliJ 13.x and I was able to create JUnit runtime configuration. You need to do the following.

In your META-INF/plugin.xml add dependency on JUnit plugin, otherwise necessary JUnit plugin classes will not be available in your plugin class loader.

<depends optional="false">JUnit</depends>

Here's the sample code to create JUnit runtime configuration. Although it works, it is just a stub, you will have to populate all attributes.

import com.intellij.execution.RunManager;
import com.intellij.execution.impl.RunManagerImpl;
import com.intellij.execution.impl.RunnerAndConfigurationSettingsImpl;
import com.intellij.execution.junit.JUnitConfigurationType;
import com.intellij.openapi.project.Project;
...
RunManagerImpl runManager = (RunManagerImpl) RunManager.getInstance(project);
JUnitConfigurationType type = JUnitConfigurationType.getInstance();
RunnerAndConfigurationSettingsImpl runnerAndConfigurationSettings = (RunnerAndConfigurationSettingsImpl)runManager.createRunConfiguration("junit test run", type.getConfigurationFactories()[0]);
runManager.addConfiguration(runnerAndConfigurationSettings, false);

And here we go, JUnit run configuration.

enter image description here

kukido
  • 10,431
  • 1
  • 45
  • 52
  • Actually I see no `com.intellij.execution.junit.JUnitConfigurationType` class. Can you please update your answer? I need to run a **test class (a class which has @Test annotation)** using its `Virtual File` (or `PsiFile`) as input from my IntelliJ Plugin. – Emadpres Nov 03 '16 at 13:59