Simple JUnit Test Case Example in Java

In the realm of software development, unit testing plays a pivotal role in ensuring the reliability and functionality of code. Java, being one of the most popular programming languages, offers a robust testing framework called JUnit. Let's delve into a simple yet comprehensive example of a JUnit test case in Java.

Junit5 base cheatsheet
Junit5 base cheatsheet

Before we dive into the example, it's crucial to understand that JUnit is an instance of the xUnit architecture for unit testing frameworks, which is a way of doing small tests of individual units of source code. It was initially developed for the Java programming language, but similar xUnit testing frameworks have been created for most modern programming languages.

Một số tính năng cần phải nhắc đến của Junit
Một số tính năng cần phải nhắc đến của Junit

Setting Up a JUnit Test Case

To create a JUnit test case, you'll first need to add the JUnit dependency to your project. If you're using Maven, add this to your pom.xml file:

JUnit 5 tutorial - Learn how to write unit tests
JUnit 5 tutorial - Learn how to write unit tests

<dependency> <groupId>junit</groupId> <artifactId>junit</artifactId> <version>4.13.2</version> <scope>test</scope> </dependency>

Creating a Simple Java Class

3 JUnit5 - Test Life Cycle Method Example || Unit Testing in Java || Jupiter API
3 JUnit5 - Test Life Cycle Method Example || Unit Testing in Java || Jupiter API

Let's start with a simple Java class named 'Calculator' that performs basic arithmetic operations.

public class Calculator { public int add(int a, int b) { return a + b; } public int subtract(int a, int b) { return a - b; } }

Writing the JUnit Test Case

🧪 Testing in Java Made Simple: JUnit Basics ✔️
🧪 Testing in Java Made Simple: JUnit Basics ✔️

Now, let's create a JUnit test case for the 'Calculator' class. Create a new class named 'CalculatorTest' with the following content:

import org.junit.jupiter.api.Test; import static org.junit.jupiter.api.Assertions.assertEquals; class CalculatorTest { @Test void testAdd() { Calculator calculator = new Calculator(); int result = calculator.add(5, 3); assertEquals(8, result); } @Test void testSubtract() { Calculator calculator = new Calculator(); int result = calculator.subtract(5, 3); assertEquals(2, result); } }

Running the JUnit Test Case

Java knowledge QUIZ
Java knowledge QUIZ

To run the test case, you can use any IDE that supports JUnit, such as IntelliJ IDEA or Eclipse. Simply right-click on the 'CalculatorTest' class and select 'Run' or 'Run with Coverage'.

If the tests pass, you should see a green bar next to each test method in the 'CalculatorTest' class, indicating that the test case passed. If a test fails, you'll see a red bar, and the IDE will provide detailed information about the failure.

JavaScript|ユニットテストツール「Jest」の使い方 - わくわくBank
JavaScript|ユニットテストツール「Jest」の使い方 - わくわくBank
Mastering Unit Testing with JUnit 5: A Comprehensive Tutorial
Mastering Unit Testing with JUnit 5: A Comprehensive Tutorial
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
Java Decision Making Cheat Sheet – Code Smarter Decisions! ☕🚀
simple junit test case example in java
simple junit test case example in java
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
Java Switch Statement Explained with Syntax and Example
Java Switch Statement Explained with Syntax and Example
Level Up Your Java 🚀 Intermediate Cheat Sheet You’ll Actually Use
Level Up Your Java 🚀 Intermediate Cheat Sheet You’ll Actually Use
Java Type Promotion, Type Casting & Conversion Notes
Java Type Promotion, Type Casting & Conversion Notes
Java TreeMap Infographic for Beginners (2025) - Quipoin
Java TreeMap Infographic for Beginners (2025) - Quipoin
Test Your Java Skills! 💻
Test Your Java Skills! 💻
Infographic: Juniper JNCIA-Junos JN0-102 Exam Syllabus Topics
Infographic: Juniper JNCIA-Junos JN0-102 Exam Syllabus Topics
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Multithreading in Java Explained | Thread Life Cycle & Creation Methods
Class and Object in Java Explained | OOP Concepts for Beginners
Class and Object in Java Explained | OOP Concepts for Beginners
Complete Java Cheat Sheet for Beginners to Advanced Developers
Complete Java Cheat Sheet for Beginners to Advanced Developers
an image of a test case template for students to use in their classroom or home
an image of a test case template for students to use in their classroom or home
Unit Testing Private Methods in Javascript
Unit Testing Private Methods in Javascript
java questions
java questions
a table that has two different types of content and text on it, with the words in
a table that has two different types of content and text on it, with the words in
JavaScript Basics | Chapter 1 📖 | Part 2
JavaScript Basics | Chapter 1 📖 | Part 2
Tricky Java Interview Questions and Answers | Basic and Advanced Level
Tricky Java Interview Questions and Answers | Basic and Advanced Level

Understanding the Assertions

In the 'CalculatorTest' class, we're using the 'assertEquals' method from the 'org.junit.jupiter.api.Assertions' class to compare the expected result with the actual result of the method under test. This is a simple form of assertion, and JUnit provides many other assertion methods for different types of comparisons.

For example, you can use 'assertTrue' or 'assertFalse' to check if a boolean expression is true or false, 'assertNull' to check if an object is null, and 'assertNotNull' to check if an object is not null.

In the world of software development, the importance of unit testing cannot be overstated. It's not just about catching bugs; it's about ensuring that your code works as expected and providing a safety net for future changes. JUnit, with its simplicity and robustness, is an invaluable tool for any Java developer.

So, go ahead, start writing those test cases, and watch your codebase grow stronger with each passing test. Happy coding!

Related Articles

Test Case Management Tools How To Write Test Cases In Zephyr Test Case Review Meeting Test Case Peer Review Process Test Matrix Example In Software Testing Manual Testing Test Plan Template Excel Manual Test Report Template Jira Test Case Export Test Case Review Checklist Regression Testing With Example