Next-Gen App & Browser Testing Cloud
Trusted by 2 Mn+ QAs & Devs to accelerate their release cycles

Learn about JUnit, its annotations, assertions, and test runners, and how to setup JUnit to run your first test.
Sri Priya
December 19, 2025
This article is a part of our Learning Hub. For more in-depth resources, check out our hub on JUnit Tutorial.
JUnit is a widely used unit testing framework. When combined with Selenium, it enhances the web application testing process. JUnit utilizes annotations to identify test methods and supports various assertions, test suites, and test maintenance.
To start implementing JUnit on your machine, it’s important to understand the basics of JUnit and its architecture. Additionally, you will need to know how to setup JUnit from scratch.
JUnit is a widely used unit testing framework for Java that ensures code reliability and early bug detection. Setting up JUnit helps developers automate tests efficiently and validate application logic from the start.
What Is the Architecture of JUnit 5?
JUnit 5 introduces a modular design that separates test writing, execution, and legacy support to make testing more flexible and scalable.
How To Use JUnit With IntelliJ IDEA?
IntelliJ IDEA simplifies writing, organizing, and running JUnit tests with built-in dependency management and test execution support.
JUnit 5 provides a modular and extensible testing framework for Java developers. Integrating it with IntelliJ IDEA enables faster setup, cleaner test management, and scalable automation making it an essential tool for modern software testing.
JUnit is a popular framework for automating unit testing in Java. It follows the principle of “Testing first, then coding,” which means you write tests before implementing the code that the tests will validate.
With this framework, you create test cases to perform unit testing. A unit test case is a block of code that verifies whether the program logic is functioning as expected.
Here are the reasons why JUnit is important for unit testing:
Note: Run web app testing using the JUnit framework with over 3000+ browser and OS combinations. Try TestMu AI Today!
Now that you understand the basics of JUnit and its importance for unit testing let’s learn about its working architecture.
JUnit 5 is organized into several modules, divided across three distinct sub-projects, each serving a specific purpose.

JUnit Platform is the foundational component for testing frameworks on the Java Virtual Machine (JVM). It provides a standard interface for test discovery and execution, supporting various build tools, IDEs, and test frameworks. It includes the TestEngine API, which allows for creating custom test engines and integrating third-party testing libraries. This architecture enables seamless execution and management of tests across different testing frameworks.
JUnit Jupiter is the new extension of JUnit. It consists of new annotations and libraries of JUnit 5 to use in the test classes by enhancing capabilities beyond those in JUnit 4. To be more clear, JUnit Jupiter consists of two parts: Jupiter API and Jupiter Engine.
JUnit Vintage provides a Test Engine to run JUnit 4 and JUnit 3 tests, allowing users to run legacy and new tests written with JUnit 5. JUnit Vintage is a supporting library within the JUnit 5 ecosystem to ensure a smooth transition from earlier versions of JUnit.
Follow the video below to learn more about JUnit architecture and how it is better than JUnit 4.
Subscribe to the TestMu AI YouTube Channel to stay updated on video tutorials related to Selenium Java, JUnit testing, and more.
Let’s learn how to setup JUnit further, including the necessary libraries and components for effective use.
Here, you will get a step-by-step guide on downloading and setting up JUnit on your machine so you can use it effectively. To do so, let’s start with the pre-requisites:
Installing the Java Development Kit (JDK) is essential to setup JUnit for automation testing. This JDK enables you to code and run Java programs. It is recommended that you install the latest version.
Below are the steps to guide you on installing Java on Windows.
java version to verify it.You have successfully setup Java on your machine by following the steps above. Next, let’s proceed to setup the JUnit environment variable.
Here, you will learn two key aspects: installing JUnit and setting up its environment variables.
Follow the steps below to complete the setup of JUnit on your machine, including downloading and installing JUnit 5.
You can add JUnit 5 to your project using a build tool like Maven or Gradle, which will handle your dependencies.
This will open a page where you can download the JUnit JAR file and view options to download the dependencies for inclusion in pom.xml. Since we are manually downloading the JUnit JAR, you must include this JAR file in your pom.xml.
Alternatively, you can download the JUnit JAR file directly from the Maven repository using this JAR file and save the JUnit JAR file in a designated location, such as C:\JUnit.
So far, we have downloaded two jar files (JUnit and Hamcrest) on our system. The files are placed in the C:\JUnit folder directory.
Now that you have completed the setup for JUnit along with the required libraries, the next step is to configure the necessary environment variables for accessing the JUnit framework.
To setup the JUnit environment variable on your machine, follow the same steps you used for setting up the Java environment variable.
Below are the steps for setting up JUnit environment variables.
Great, you have completed the setup of JUnit on your machine. Next, learn to configure and use JUnit in an IDE like IntelliJ.
IntelliJ IDEA is a popular cross-platform Integrated Development Environment (IDE) developed by JetBrains. It supports Java, Kotlin, Scala, Groovy, and various other languages through plugins, including PHP, Python, Ruby, and JavaScript. IntelliJ IDEA is suitable for various development needs and is available in an Apache-licensed Community Edition and a proprietary Commercial Edition.
Once you’ve completed the setup of JUnit on your machine, you can leverage IntelliJ IDEA to write and manage more complex test code. This IDE simplifies using the JUnit framework, allowing you to write, execute, and manage your tests efficiently.
To get started, follow these steps for the setup of JUnit in IntelliJ:
Alternatively, If you’re using Maven or Gradle, add JUnit dependencies to your pom.xml or build.gradle file respectively.
For Maven:
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.8.2</version> <!-- Use the latest version -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version> <!-- Use the latest version -->
<scope>test</scope>
</dependency>
</dependencies>
For Gradle:
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.2' // Use the latest version
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.2' // Use the latest version
}
Note: If not using a build tool, download the JUnit JAR files and add them to your project’s classpath.
After completing the setup of JUnit, it is time to create your first test. Writing a basic code will help you understand how JUnit works and validate that your setup is correct.
The below class has a method that converts Fahrenheit to Celsius.
package com.lambdatest.junit;
public class Class1 {
public double conversion(double temparature, String unit){
if(unit.equals("F")){
return (temparature-32)*(5.0/9.0);
}else{
return (temparature *(9.0/5.0))+32;
}
}
}
Now, let us write the simple JUnit test class to test the conversion() in Class 1.
package com.lambdatest.junit;
import org.junit.Test;
import static junit.framework.Assert.assertEquals;
public class JunitTestClass1 {
@Test
public void testconversion(){
//Given
Class1 classunderTest=new Class1();
//When
double temparature=80.0d;
String unit="";
double result = classunderTest.conversion(temparature, unit);
//Then
//assertions
assertEquals(170.00d,result,0.0);
}
}
The JUnit annotation @Test tells JUnit that the public void method it is attached to can be run as a test case.
We split the JUnit test class into three parts: Given, When, and Then.
Here:
To learn more about JUnit assertion, watch the video below and get detailed insights.
There are two ways to setup JUnit on IntelliJ IDEA:
Here is an example snippet from pom.xml showing how to include local dependencies:
<dependencies>
[...]
<dependency>
<groupId>hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<version>2.2</version>
<scope>system</scope>
<systemPath>C:JUnithamcrest-2.2.jar</systemPath>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.1</version>
<scope>system</scope>
<systemPath>C:JUnitjunit.jar</systemPath>
</dependency>
[...]
</dependencies>
Double-click on any JUnit import statement to verify that the local dependencies are correctly included (e.g., import org.junit.AfterClass;). This will confirm if the local dependencies are fulfilled as specified in the pom.xml.
Dependencies can be added at three levels in IntelliJ IDEA:
To install JUnit and Hamcrest JAR files as external libraries at the ‘Project Level’:
With the above steps, you have completed the setup of JUnit in IntelliJ. Now, let’s learn how to execute your JUnit tests within the IntelliJ platform.
To run JUnit tests in IntelliJ IDEA, follow the below steps:

JUnit 5 supports parallel execution of tests, which speeds up the execution of the test suite when there are a large number of tests. There are two ways to achieve parallel execution in JUnit 5: at the class and method levels. Below, you can see how to configure parallel execution for both approaches.
<properties>
<junit.jupiter.execution.parallel.enabled>true</junit.jupiter.execution.parallel.enabled>
</properties>
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.junit.jupiter.api.parallel.ExecutionMode;
class ParallelMethodTest {
@Test
@Execution(ExecutionMode.CONCURRENT)
void test1() {
// test logic
}
}
package com.test.junitdemo;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.jupiter.api.parallel.Execution;
import org.openqa.selenium.By;
import org.openqa.selenium.chrome.ChromeOptions;
import org.openqa.selenium.remote.RemoteWebDriver;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.HashMap;
import static org.junit.jupiter.api.parallel.ExecutionMode.CONCURRENT;
@Execution(CONCURRENT)
public class JUnitDemoClass {
public String username = "username";
public String accesskey = "access_key";
public static RemoteWebDriver driver;
public String gridURL = "@hub.lambdatest.com/wd/hub";
boolean status = false;
static String URL = "https://ecommerce-playground.lambdatest.io/";
@Before
public void setup() throws MalformedURLException {
String hub = "@hub.lambdatest.com/wd/hub";
ChromeOptions browserOptions = new ChromeOptions();
browserOptions.setPlatformName("Windows 11");
browserOptions.setBrowserVersion("126.0");
HashMap<String, Object> ltOptions = new HashMap<String, Object>();
ltOptions.put("username", "username");
ltOptions.put("accessKey", "acceeskey");
ltOptions.put("project", "Junit Test");
ltOptions.put("w3c", true);
ltOptions.put("plugin", "java-testNG");
browserOptions.setCapability("LT:Options", ltOptions);
try {
driver = new RemoteWebDriver(new URL("https://" + username + ":" + accesskey + gridURL), browserOptions);
} catch (MalformedURLException e) {
System.out.println("Invalid grid URL");
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Test
public void test_() throws InterruptedException {
System.out.println(Thread.currentThread().getName());
driver.navigate().to(URL);
driver.manage().window().maximize();
try {
driver.findElement(By.xpath("(//input[@placeholder='Search For Products'])[1]")).sendKeys("iPhone");
driver.findElement(By.xpath("//button[normalize-space()='Search']")).click();
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, "Search IPhone");
if (actualTitle.equals("Search-iphone")) {
System.out.println("Demonstration of running JUnit tests is complete");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@Test
public void first() throws Exception{
System.out.println("FirstParallelUnitTest first() start => " + Thread.currentThread().getName());
Thread.sleep(500);
System.out.println("FirstParallelUnitTest first() end => " + Thread.currentThread().getName());
}
@Test
public void second() throws Exception{
System.out.println("FirstParallelUnitTest second() start => " + Thread.currentThread().getName());
Thread.sleep(500);
System.out.println("FirstParallelUnitTest second() end => " + Thread.currentThread().getName());
}
@Test
public void test_1() throws InterruptedException {
System.out.println(Thread.currentThread().getName());
driver.navigate().to(URL);
driver.manage().window().maximize();
try {
driver.findElement(By.xpath("(//input[@placeholder='Search For Products'])[1]")).sendKeys("iPhone");
driver.findElement(By.xpath("//button[normalize-space()='Search']")).click();
String actualTitle = driver.getTitle();
Assert.assertEquals(actualTitle, "Search IPhone");
if (actualTitle.equals("Search-iphone")) {
System.out.println("Demonstration of running JUnit tests is complete");
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
@After
public void tearDown() {
driver.quit();
}
}

junit.jupiter.execution.parallel.config.strategy=fixed
junit.jupiter.execution.parallel.config.fixed.parallelism=4
Did you find this page helpful?
More Related Hubs
TestMu AI forEnterprise
Get access to solutions built on Enterprise
grade security, privacy, & compliance