0

I have created an project with "JUnit Testing Framework" in Eclipse. Instead of running the program thro' Eclipse, i tried to run from external JAVA program.

I'm able to run Junit Project from Cmd line thro'

"java -cp "D:\Junit-4.10.jar" org.junit.runner.JUnitCore com.sample.mytestcase

I tried to use this above command in external JAVA program; like

Runtime.getRuntime().exec("java -cp 'D:\Junit-4.10.jar' org.junit.runner.JUnitCore com.sample.mytestcase");

Nothing seems to worked. Is there any other methods to trigger JUNIT project from another JAVA programs? Thanks in Advance.

msg
  • 1,480
  • 3
  • 18
  • 27
  • 1
    I'm guessing the result of that command is a bunch of data on stdout? If so, then you probably aren't reading the output. See [java runtime.getruntime() getting output from executing a command line program](http://stackoverflow.com/q/5711084) for inspiration. – Duncan Jones Sep 22 '14 at 14:46
  • Why do you need to run jUnit test cases via command line? – Tassos Bassoukos Sep 22 '14 at 15:00
  • Hi @Duncan, i tried the method as suggested by you. Here i am able to get result from command_Prompt for various commands. But above mentioned java/junit command doesn't return any result back.(For few commands i'm not getting any result back) – msg Sep 23 '14 at 05:05
  • @TassosBassoukos, my reqiurement is to run Junit testcase from outside Eclipse environment. – msg Sep 23 '14 at 05:07
  • I'm getting results back from cmd prompt for "java -jar D:\sample.jar". But nothing returned for commands like "java -version", "java -cp "D:\Junit-4.10.jar" org.junit.runner.JUnitCore com.sample.mytestcase" – msg Sep 23 '14 at 05:09

1 Answers1

0

Use the JUnitCore class to do it : http://junit.sourceforge.net/javadoc/org/junit/runner/JUnitCore.html

JUnitCore junit = new JUnitCore();
Result result = junit.runClasses(testClasses); // testClasses is the array of test classes to be executed
Kraal
  • 2,779
  • 1
  • 19
  • 36
  • Anyway after adding this command, i need to run program from eclipse environment. My requirement is to run from outside Eclipse(thro' another JAVA program would be fine) – msg Sep 23 '14 at 05:14
  • You have to use JUnitCore in the java program that launches your junit tests. You don't have to use Runtime.exec... Read the documentation. – Kraal Sep 23 '14 at 05:41