How to Compile and Run JUnit Test Class from Command Line
The use of IDE blunts developers due to several reasons because IDEs take many responsibilities from developers especially, compile error detection, early checked & unchecked exception detection and many boilerplate codes’ can be easily added by IDE, etc so in my personal projects, I prefer to code with notepad rather than IDEs.
If you wonder how to compile and run java app with command line, you can check out this content “How to Compile and Run Java Program in Command Line”. Today, I will explain how to run a test class of had been wrote class.
Firstly, let’s create a Java code. For this java source code, I used a singleton class that name Earth. Additionally, If you wonder that “what is the singleton?”, you can check out this content “Singleton Design Pattern”.
With Earth class, some variables and some methods were defined. Basically, this class is used for modelling Earth planet. There is a population variable and we can be changed by using some methods. Now let’s create this class’ test class.
Basically, this class checks taking instances’ equality. There is a question that how to run this test class in command line?.
Firstly, you must have some jars that names JUnit and Hamcrest All. You can download from https://mvnrepository.com/.
Secondly, you must compile your test class. For that, you can use this command.
javac -cp [junit jar with absolute path];[hamcrest-all jar with absolute path];. <TestClasss>.java
As you know javac command is used for compilation java sources also I added an options as named -cp to javac next to. What is the -cp option ? Oracle Doc. explains -cp option very well.
According to Java Doc,
-classpath path, or
-cp path, Specifies where to find user class files and annotation processors. This class path overrides the user class path in the
CLASSPATH environment variable.
-cpare not specified, then the user class path is the value of the
CLASSPATHenvironment variable, if that is set, or else the current directory.
- If not compiling code for modules, if the
--source-pathor -sourcepath` option is not specified, then the user class path is also searched for source files.
- If the
-processorpathoption is not specified, then the class path is also searched for annotation processors.
Thirdly, for running your test class, you can use this command
java -cp [junit jar with absolute path];[hamcrest-all jar with absolute path];. org.junit.runner.JUnitCore EarthTest
Unlike the other I added org.junit.runner.JUnitCore. Why I use to this ?
According to JUnitCore documentation, JUnitCore is a facade for running tests. It supports running JUnit 4 tests, JUnit 3.8.x tests, and mixtures.
To run tests from the command line, run java org.junit.runner.JUnitCore TestClass1 TestClass2 …. For one-shot test runs, use the static method runClasses(Class). If you want to add special listeners, create an instance of JUnitCore first and use it to run the tests.
I hope everything is clear for you. If you don’t understand any part, please feel free, ask me also if you want to do practice, you can find many test classes in my GitHub, especially you can check out my design-patterns repository.
Additionally, I used various resources for prepare this essay. I indicated in following. You can check out.