Published on Fri Jul 25 2025 10:30:00 GMT+0000 (Coordinated Universal Time) by Purusothaman Ramanujam
Unit Testing in Java with JUnit 5: A Beginner’s Guide
Introduction
Testing is a crucial part of software development. Unit tests help ensure your code works as expected and make it easier to maintain and refactor. JUnit 5 is the latest version of the most popular testing framework for Java, bringing many improvements and new features.
In this post, you’ll learn how to write and run your first unit tests using JUnit 5.
What is JUnit 5?
JUnit 5 is a modern, flexible, and powerful testing framework for Java. It consists of three main modules:
- JUnit Platform: Launches testing frameworks on the JVM
- JUnit Jupiter: New programming model and extension model for writing tests
- JUnit Vintage: Supports running JUnit 3 and 4 tests
Adding JUnit 5 to Your Project
Maven Dependency
<dependency> <groupId>org.junit.jupiter</groupId> <artifactId>junit-jupiter</artifactId> <version>5.10.0</version> <scope>test</scope></dependency>Gradle Dependency
testImplementation 'org.junit.jupiter:junit-jupiter:5.10.0'Writing Your First Test
Let’s write a simple class and test it.
public class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; }}Now, let’s write tests for this class:
import org.junit.jupiter.api.Test;import static org.junit.jupiter.api.Assertions.*;
class CalculatorTest { @Test void testAdd() { Calculator calc = new Calculator(); assertEquals(5, calc.add(2, 3)); }
@Test void testSubtract() { Calculator calc = new Calculator(); assertEquals(1, calc.subtract(3, 2)); }}@Testmarks a method as a test case.assertEquals(expected, actual)checks if the result matches the expectation.
Running Tests
Most IDEs (IntelliJ IDEA, Eclipse, VS Code) let you right-click and run tests. You can also run them from the command line:
- Maven:
mvn test - Gradle:
./gradlew test
More JUnit 5 Features
Assertions
JUnit 5 provides many assertions:
assertTrue(condition);assertFalse(condition);assertNull(value);assertNotNull(value);assertThrows(Exception.class, () -> { ... });Parameterized Tests
Run the same test with different inputs:
import org.junit.jupiter.params.ParameterizedTest;import org.junit.jupiter.params.provider.ValueSource;
class ParameterizedExample { @ParameterizedTest @ValueSource(strings = {"racecar", "radar", "level"}) void testPalindrome(String word) { assertTrue(isPalindrome(word)); } boolean isPalindrome(String str) { return new StringBuilder(str).reverse().toString().equals(str); }}Test Lifecycle Methods
Set up or clean up before/after tests:
import org.junit.jupiter.api.BeforeEach;import org.junit.jupiter.api.AfterEach;
class LifecycleExample { @BeforeEach void setUp() { // Runs before each test } @AfterEach void tearDown() { // Runs after each test }}Best Practices
- Test Small Units: Test one method or behavior at a time.
- Name Tests Clearly: Use descriptive method names.
- Keep Tests Independent: Each test should run on its own.
- Use Setup Methods: Use
@BeforeEachfor common setup code. - Test Edge Cases: Don’t just test the happy path.
Conclusion
JUnit 5 makes unit testing in Java easy and powerful. Start by writing simple tests, then explore advanced features like parameterized tests and lifecycle methods. Good tests help you write better, more reliable code.
Resources
Written by Purusothaman Ramanujam
← Back to blog