Introduction
In this chapter, we will guide you through writing your first JUnit test step by step. We will create a simple Java class, write a test for it, and run the test to ensure everything is working correctly.
Let’s first take a look into some basic concepts related to JUnit tests.
Basic Concepts
1. Test Class
A test class in JUnit is a regular Java class that contains one or more test methods. These test methods are annotated with @Test
and contain the logic to test a specific piece of code.
2. Test Method
A test method is a method within a test class that is annotated with @Test
. This method contains assertions that check the expected outcomes of the code being tested.
3. Assertions
Assertions are used to check whether the code under test behaves as expected. JUnit provides several assertion methods, such as assertEquals
, assertTrue
, assertFalse
, and assertThrows
.
4. Test Runner
The test runner is responsible for executing the test methods in a test class and reporting the results. In JUnit, the test runner is typically provided by an IDE or build tool, such as Eclipse, IntelliJ IDEA, Maven, or Gradle.
Step 1: Create a Simple Java Class
First, create a simple Java class that we will test. For this example, let’s create a Calculator
class with a method to add two numbers.
- Create a New Java Project: Open your IDE and create a new Java project.
- Create a New Java Class: Inside your project, create a new Java class named
Calculator
.
public class Calculator {
public int add(int a, int b) {
return a + b;
}
}
Step 2: Add JUnit to Your Project
Depending on your build tool, you will need to add the JUnit dependency to your project.
Using Maven
Add the JUnit dependency to your pom.xml
file:
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.8.2</version>
<scope>test</scope>
</dependency>
Using Gradle
Add the JUnit dependency to your build.gradle
file:
dependencies {
testImplementation 'org.junit.jupiter:junit-jupiter-engine:5.8.2'
}
Step 3: Create a Test Class
Next, create a test class for the Calculator
class.
- Create a New Test Class: In your project, create a new Java class named
CalculatorTest
in thesrc/test/java
directory.
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class CalculatorTest {
@Test
public void testAdd() {
Calculator calculator = new Calculator();
int result = calculator.add(2, 3);
assertEquals(5, result, "2 + 3 should equal 5");
}
}
Explanation of the Test Class
@Test
: This annotation indicates that the methodtestAdd
is a test method.assertEquals(5, result, "2 + 3 should equal 5")
: This assertion checks if the result ofcalculator.add(2, 3)
is equal to 5. If the result is not 5, the test will fail with the message “2 + 3 should equal 5”.
Step 4: Run the Test
Now that you have written your test, you need to run it to ensure everything is working correctly.
Using Eclipse
- Run Test: Right-click on the
CalculatorTest
file or thetestAdd
method and selectRun As
>JUnit Test
. - View Results: The results will be displayed in the JUnit view. A green bar indicates the test passed, while a red bar indicates it failed.
Using IntelliJ IDEA
- Run Test: Click the green run icon next to the
testAdd
method or theCalculatorTest
class and selectRun
. - View Results: The results will be displayed in the Run window. A green check mark indicates the test passed, while a red cross indicates it failed.
Using VS Code
- Run Test: Open the
CalculatorTest
file and click theRun
icon above thetestAdd
method. - View Results: The results will be displayed in the Test Explorer. A green check mark indicates the test passed, while a red cross indicates it failed.
Conclusion
You have successfully written and run your first JUnit test. This process involves creating a simple Java class, writing a test class with test methods, and running the test to verify that your code works as expected. This foundational skill will enable you to write more comprehensive tests for your Java applications.