Multiple Ways To Execute JUnit Tests

By Sruthy

By Sruthy

Sruthy, with her 10+ years of experience, is a dynamic professional who seamlessly blends her creative soul with technical prowess. With a Technical Degree in Graphics Design and Communications and a Bachelor’s Degree in Electronics and Communication, she brings a unique combination of artistic flair…

Learn about our editorial policies.
Updated March 7, 2024

This Tutorial shows Multiple ways of Executing JUnit tests such as Running as a JUnit Test, Using Shortcut Keys, or Run JUnit Test from Command-Line, etc:

We saw how to write the basic JUnit test cases and have a test fixture approach as one of the good programming practices for JUnit in our previous tutorial.

In this tutorial, let’s have a look at the different ways that we can use to execute tests for JUnit. You will be surprised to look at the number of options available to run these test cases.

=> Watch Out The Simple JUnit Training Series Here.

Multiple Ways to Execute JUnit Tests

Different Ways To Execute JUnit Tests

In order to execute JUnit tests, there are certain ways wherein you could run a single class file with one or multiple test method(s) through the following options:

  1. ‘Run as JUnit test’ option.
  2. Run last executed JUnit test through the menu option.
  3. Run using shortcut keys.
  4. Run only one test method in a class.
  5. Run through the command line.
  6. Run using Testrunner class file.
  7. Run using through Maven as well.

Note: The JUnit test execution through Maven will be contemplated in a separate tutorial for JUnit Maven.

Reiterating the point, in this tutorial, we will learn how multiple tests can be grouped together into a test suite and how could one run the suite in different ways. Besides, it shall detail certain relevant and related additional information.

#1) Run As JUnit Test

The simplest way in which you can execute the JUnit tests is:

Method 1:

  1. Right-click on the class file in the Script view
  2. Select Run As -> JUnit Test
  3. The class file executes.

Method 2:

  1. Similarly, you may select the class file from the Package Explorer view
  2. Right-click the file
  3. Select Run As -> JUnit Test
  4. The class file executes.

Note: This way you could execute one class file at a time.

Run as JUnit test

#2) Run Last Executed JUnit Test Via The Menu Option

You may run a JUnit class file by keeping a class file open in the editor. Go to the top menu of the Eclipse => Select Run => Run. The option Run ->Run basically means rerun the test that you ran last.

Let’s consider a JUnit class with multiple methods/tests for better clarity on how Run->Run works:

  • Scenario 1: If you had run a single method with @Test, then when you click Run->Run, the single method that ran last would only run this time and not the entire JUnit class.
  • Scenario 2: Whereas had you run the entire class previously, Run->Run would rerun the entire class file.

Now that we know Run->Run runs the test that you ran last, this brings us to a question if you can change the preference of the Run->Run option?

The answer to the question is Yes, the preference of the Run->Run option can be changed. There is a certain configuration attached to Run->Run.

Here is how you can do that:

a) Eclipse’s run setting basically defaults to run the selected resource or active editor if it’s launchable.

So, what does the default setting – ‘run the selected resource or active editor if its launchable’ do?

The answer to this is that it won’t execute the application that you launched last, rather it will follow the rerun of the last launched application for the active editor.

b) Then how do you change the default preference?

The answer to this is that you can change the default preference in Eclipse to run the last application you launched irrespective of the active editor you have.

Below is how you change the preference of Run option using Run -> Run:

  • Navigate to Windows => Preferences => Run/Debug => Launching
  • ‘Launch Operation’ has a default radio button – Launch the previously launched application’ selected under the second option ‘Launch the selected resource or active editor. If not launchable:’.
  • You might have to change this preference to the first radio button i.e. Always launch the previously launched application’.

2. Run last executed JUnit test via Menu option

#3) Run Using Shortcut Keys

You may select the class file from the Script view or Package Explorer view, and use the below shortcut keys to execute the JUnit tests:

  1. Press the keys ALT+SHIFT+X, T in order to execute the JUnit class file.
  2. An alternative to this would be press ALT+R then CTRL+F11 to execute a JUnit class file. ALT+R then CTRL+F11 is the shortcut for menu option Run -> Run

#4) Run Only One Test Method In A Class

Sometimes, you may want to run a single JUnit test method.

In case, there is more than one method inside the JUnit class file:

  1. You may select or place your cursor on the method’s name inside the script view.
  2. Use either shortcut keys mentioned above or the options provided above to execute only the method you just selected.

Note: ALT+SHIFT+X, T may run selected methods as expected. However, if you wish to run a specific method in a JUnit class, it must be a testcase annotated with @Test else it shows initialization error.

In other words, if you select methods under @Before or @After (any annotation other than @Test), then the specific method execution would error.

#5) Run JUnit Tests From Command Line

Like you run any Java class files via command line, you can also compile and run JUnit class files via the command line.

We will cover the below sub-topics here to get an understanding of how can we run JUnit tests through the command line:

  1. How to compile a JUnit test in the command line?
  2. How to run a JUnit test in the command line?
  3. Additional information on command-line execution.
    • How to fix unrecognized command error for javac command?
    • Advantages of running tests using the command line.

#5.1) How to compile a JUnit test in the command line?

The precondition to compiling and running a JUnit class file via command prompt is:

  1. First add relevant JUnit jar files into the classpath.
  2. Set the environment variables as it was mentioned in the SetUp of JUnit tutorial.
  3. Then compile a JUnit class file.
  4. The syntax for compiling a JUnit class file through the command line is:
javac -cp junit-4.0.0.jar;. JUnitProgram.java

Here, javac is the Java compiler that uses -cp option.

The command javac -cp looks for the following parameters:

  1. The JUnit jar file is followed by a semicolon.
  2. The path of the directory in which the source file exists.
  3. The class file name

In the above-given syntax, what does the dot (.) imply?

We have mentioned a dot in the place of the entire path of the directory.

The dot implies that:

  1. The classpath already includes the current directory for the Java source files.
  2. The JVM (Java Virtual Machine) automatically assumes that the current directory is where the source files are placed.
  3. JVM then searches for the mentioned JUnit file name there. The filename is the last parameter given in the compile command.

You may check the parameters that go into -cp through the following steps:

  1. Open the command prompt.
  2. Type javac and press ENTER.
  3. All relevant options show up including -cp. You will find that -cp goes with <path> as a parameter where the path is the class files path that JVM searches for.

Screenshot below:

compile a JUnit test in command line

How To Compile Multiple Files At Once?

Multiple JUnit test files can be compiled at once by separating the file names with spaces.

Given below is an example of where you compile java files JUnitProgram and demoTest:

javac -cp junit-4.0.0.jar;. JUnitProgram.java demoTest.java

#5.2) How to Run a JUnit Test From Command Line?

Just like javac is the Java compiler used, similarly java -cp is used to run the Java class files including the JUnit classes.

Below is the syntax that you could follow:

java -cp junit-4.0.0.jar;. JUnitProgram demoTest

This command executes both the files JUnitProgram.java and demoTest.java one after the other.

#5.3) Additional Information on ‘command-line execution’.

Here is some additional information on how to fix an error with javac command and why use command-line run option

#5.3.1) How do I fix the unrecognized command error for javac command?

Most of us would encounter this issue while trying to execute the javac command through the command line. This has happened to me as well; so we thought of penning it here.

a) We entered the command javac and pressed Enter on the command prompt.

b) The error message – javac is not recognized as an internal or external command, operable program or batch file showed up as below:

Error message

This is where your compilation of the Java class files from the command line begins. Hence, the error is indeed a matter of concern and can’t be ignored.

In order to fix the issue, follow the below steps and Voila!!! you see the error is gone:

  • Let’s demo this process using a basic Java file. The first step you could do is create a basic Java class E.g.: “Calculator.java”
  • We shall locate the Calculate.java from the Windows Explorer and copy the path.

Calculate.java from the Windows Explorer

  • Change the directory in the command prompt to the path you copied (the source file path). Use cd <source file path> to change the directory.

3. Change the directory in the command prompt to the path you copied

  • Now set the PATH to the jdk bin folder using the command.

SET PATH=<jdk bin folder path> and press ENTER.

  • Here, the jdk path is C:\Program Files\Java\jdk1.8.0_181\bin. Hence, we have set the path accordingly. The resultant show nothing on pressing ENTER after the command.

The resultant shows nothing on pressing ENTER

  • Now, verify if the JVM recognizes the command javac by entering the command javac and pressing ENTER.
    1. If it does recognize the command, then a set of valid options for javac displays as the resultant.
    2. Else the error will show up again.

Given below is a screenshot showing that we successfully got rid of the error.

command javac

Let’s not try to elude an essential question here:

Why did the JVM recognize javac command after setting the path to the jdk bin folder?

We are sure that you will have this question in your mind too. Given below is the answer.

  • The jdk bin folder has all the libraries for javac command. Hence, this is why, when you set the path accordingly, the JVM is now able to recognize the javac command without any issue.
  • See the javac folder under the jdk bin in the below image.

javac folder under the jdk bin

  • You may then, run the’ Java compile and run’ command using the command line. Besides, also remember to set the CLASSPATH variable appropriately. JAVA_HOME and JUNIT_HOME variable for Java files and JUnit files, respectively.

#5.3.2) Advantage Of Running Tests Using The Command Line:

Let’s quickly discuss, the advantage over running Java/JUnit testcases via the command line.

As you are already aware, there is no hard and fast rule on the execution of the class files being through the command line. It is just an alternative way, on how you can manage the compilation and execution of the class files.

If you ask if there is a special advantage in having know-how on the execution of the JUnit tests via command line, then, we would say ‘Certainly, Yes’.

The reason for a ‘Yes’ is given below:

  1. All these series of steps that we followed above; could be added into notepad and converted to a batch file.
  2. Now, when you run this batch file with a double click, it could trigger the compilation and execution of multiple JUnit test files named in the batch file.

What is the benefit of having a batch file do the compile and execution of the Java files?

  1. A batch/jar file might act like a user-friendly utility that could enable anyone unaware of the internal logic of the code, and execute multiple test cases very easily.
  2. This may eliminate the need to have a specialized developer or QA to do these test execution jobs. The execution task can be delegated to any resource without bothering about skill constraints.

In the next alternative option, we will see another advantageous and commendable way of executing our JUnit test cases.

#6) Run Test Suite Using Testrunner Class

In real-time scenarios, executing one testcase at a time is the least preferred option.

  • We have cases where-in we need to run a group of related/unrelated test cases.
  • For instance, we might need to create and execute regression test suites or smoke test suites.

We will now learn about the implementation of different annotations used to create test suites and execute the suite.

The overall process of executing the test suite using Test Runner is as per the below workflow:

  1. Create JUnit class 1, JUnit class 2, …. JUnit class n.
  2. Create Test suite class file grouping the test cases.
  3. Create a Testrunner class file to invoke the Test suite created.
  4. Execute the Testrunner class.

The structure of the programs through which we shall demo the creation of test suite and execution of the runner file is shown in the below image:

test suite

Here, we will cover the sub-topics:

  1. Creating JUnit Classes
  2. Creating Test Suites
  3. Creating a Testrunner file and executing the test suites using it.
  4. Additional information on the working of @RunWith annotation.

#6.1) Creating JUnit Classes

Let’s start by creating two simple JUnit class files:

  1. JUnitTestCase1.java – It includes the code to verify an expected numerical value – the variable Value1 matches an actual value of the variable Value2.
  2. JUnitTestCase2.java – Includes the code to verify if the expected string variable strValue and actual string variable strActual matches.

These are basically two test cases that we will try to get into a logical grouping called test suite and make it run one after the other.

Code For JUnitTestCase1.java

package demo.tests;
import static org.junit.Assert.*;
import java.util.*;
import java.lang.String;
import static org.testng.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import junit.framework.Assert;
public class JUnitTestCase1 {
	public int Value1=6000;
	@Test
	 public void junitMethod1(){
		int Value2=9000;
		Assert.assertEquals(Value1, Value2);
	   }
}

Code For JUnitTestCase2.java

package demo.tests;
import static org.junit.Assert.*;
import java.util.*;
import java.lang.String;
import static org.testng.Assert.assertTrue;
import org.junit.Before;
import org.junit.Test;
import junit.framework.Assert;
public class JUnitTestCase2 {
	public String stringValue="JUnit";
	@Test
	 public void junitMethod2(){
		String strActual="Junit1";
		Assert.assertSame(stringValue, strActual);
	   }
}

#6.2) Creating Test Suite:

This section and the next section play a prominent role in the entire process of creating and running a test suite. In this section, we will try to understand how to group together multiple JUnit test classes and bind them into a test suite.

As per the structural image above, let’s create a test suite grouping together JUnitTestCase1.java and JUnitTestCase2.java and name the suite as JUnitTestSuite.java

The two annotations that help us achieve the creation of a test suite is:

  1. @RunWith and
  2. @SuiteClasses

Packages needed for the annotations:

  1. You will need to import the package org.junit.runner.RunWith; for the inclusion of @RunWith annotation.
  2. You will need the package org.junit.runners.Suite.SuiteClasses for @SuiteClasses to work.
  3. Besides, you will also need to import the package org.junit.runners.Suite for passing a parameter Suite.class into the annotation @RunWith.

Let’s look into the code for better understanding!!

Code for JUnitTestSuite.java

package demo.tests;
import static org.junit.Assert.*;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

@RunWith(Suite.class)
@SuiteClasses({JUnitTestCase1.class, JUnitTestCase2.class })

public class JUnitTestSuite {
@BeforeClass
	public static void printMe() {
		System.out.println("JUnitTestSuite is the test suite grouping 		testcase 1 and testcase 2");

}
}

Understanding of the code for JUnitTestSuite.java:

  1. @RunWith helps the JVM understand what kind of runner class should it run E.g. Suite.class or Cucumber.class
  2. Here, the parameter of @RunWith is Suite.class. It helps JVM recognize that the current file where the @RunWith(Suite.class) is used plays a role in the Test Suite.
  3. The JUnit test class names to be bound together in a suite must be passed as a string array in the form of parameters for @SuiteClasses each separated by a comma.
  4. This enables JVM to know which are all the testcases that need to be grouped under the suite.
  5. The suite name will be the JUnit class file name that is annotated with @RunWith and @SuiteClasses which is JUnitTestSuite in this case.

#6.3) Create Test Runner file and Run JUnit Test suite using Test Runner

The last step will help us to run the test suite that we just created in the above section using a Testrunner file.

  1. We will now create a Java file named SuiteRunnerFile.
  2. This SuiteRunnerFile.java is not a JUnit class but a usual Java file with the main method in it.

Let’s look at the code and then try to understand it.

Code for SuiteRunnerFile.java

package demo.tests;

import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;

public class SuiteRunnerFile {
public static void main(String args[]) 
{
	Result result=JUnitCore.runClasses(JUnitTestSuite.class);
	for (Failure failure : result.getFailures())
		{
	    		System.out.println(failure.toString());
		}
                  }
}

Packages Needed for the Annotation

  1. You need to import the package org.junit.runner.JunitCore in order to include the JUnitCore class in the code.
  2. You need to import the package org.junit.runner.notification.Failure and org.junit.runner. Result to include Failure and Result class in the code, respectively.

Understanding of the Code for SuiteRunnerFile.java

  1. In order to create a runner file for the test suite execution, the JUnitCore class plays a significant role.
  2. The runClasses() method of JUnitCore class takes the test suite class name as the input parameter hence we have the statement JUnitCore.runClasses(JUnitTestSuite.class).
  3. The return type of this statement is the Result class object that stores the resultant success status and failure status of each of the test case file; post-execution. This is why we have a result as the Result class object in the code.
  4. Then we print the failures of the test cases if any. Like getFailures() method, you may also get the failure count and Run count using the method getFailureCount() and getRunCount(), respectively.
  5. Now SuiteRunnerFile is ready to execute,
    1. Select the file from the Package Explorer and
    2. Right-click and select Run As -> Java, the program executes.

Given below is the screenshot of the Console window.

Console window

Explanation of the Results on the console:

The console above shows that:

  1. The JUnitTestSuite class file has executed through SuiteRunnerFile.
  2. The printMe() method under annotation @BeforeClass executed first and
  3. Then the test cases in the test suite executed one after the other. This is how the test suite can be created and run as a package.

#6.4) Additional Info – How does @RunWith work?

  • @RunWith is a JUnit API that basically takes only one element as an input parameter that is a runner class file name.
  • JUnit framework invokes the specified class as a test runner.

The below snippet from RunWith.java will help you get a grasp:

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@Inherited
public @interface RunWith {
Class&amp;lt;? Extends Runner&amp;gt; value();
}

Understanding the above RunWith interface code:

  1. The specified value element must be a derived class of the Runner class. The concept of reflection is used here.
  2. A very good example of such a runner class is already implemented in our code i.e. @RunWith(Suite.class) where a group of testcases is bound together to make a test suite.
  3. Similarly, another good example of using a Runner class with @RunWith could be @RunWith(Cucumber.class) which is a business-driven development (BDD) framework for test automation using Selenium in Java. This helps the framework run the Cucumber based test cases.

Note:

  • The annotations and parameters used to create and run the JUnit test suite in this tutorial were specific to JUnit 4.
  • There is a slightly different way of how you create a JUnit Test Suite and execute the runner file in JUnit 5.

We will have a focussed understanding of all the aspects of JUnit 4 vs JUnit 5 soon in our upcoming tutorials.

#7) Run JUnit Test Cases Using Maven

You can also have a Maven project consisting of JUnit tests in place and run the tests through Maven which will be covered in a separate tutorial.

Conclusion

  1. We learned all the different options for running the JUnit tests – single tests as well as multiple ones grouped together into test suites.
  2. We got additional knowledge on how to update the preference for the Run option, how to fix javac error, and how could command line execution help us.
  3. Besides, we also learned about how @RunWith annotation works.

Hence, there is more to follow in the upcoming tutorials. ‘Stand By’ until then!!!

=> Visit Here To Learn JUnit From Scratch.

Was this helpful?

Thanks for your feedback!

Leave a Comment