7

Possible Duplicate:
How to run Junit testcases from command line?

I've run my JUnit tests using maven before. Now I'm packaging all my source code into a JAR file, and want to run it using a java command. How can I do that? Note that there is no main class in my code.

0

1 Answer 1

17

You need to make sure the classpath contains

  1. Your JAR
  2. The JUnit JAR

You can set the class path by using the -cp flag to the java command. Then you can use junit.textui.TestRunner to run the tests.

If you're using Linux (note the use of : as the path separator between jars)

java -cp /path/to/my.jar:/path/to/junit.jar junit.textui.TestRunner com.mypackage.MyClassWithUnitTests

If you're using Windows (note the use of ; as the path separator between jars)

java -cp /path/to/my.jar;/path/to/junit.jar junit.textui.TestRunner com.mypackage.MyClassWithUnitTests
Sign up to request clarification or add additional context in comments.

2 Comments

its not junit.textui.TextRunner, but junit.textui.TestRunner
For Junit4 you can use this: java -cp /path/to/my.jar;/path/to/junit.jar org.junit.runner.JUnitCore com.mypackage.MyClassWithUnitTests

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.