The assertEquals
method in JUnit is used to test if two values are equal. JUnit provides several overloaded versions of this method to handle different data types and to provide custom messages for test failures. This guide covers the basics of using the assertEquals
method, including its syntax and examples of its different overloads.
Table of Contents
- Introduction
assertEquals
Method Syntax- Examples
- Basic Usage
- Comparing Strings
- Comparing Arrays
- Providing a Custom Message
- Handling Floating Point Numbers
- Real-World Use Case
- Conclusion
Introduction
The assertEquals
method in JUnit is an assertion method used to verify that two values are equal. If the values are not equal, the assertion fails, and the test is marked as failed. This method is fundamental for writing unit tests that check the correctness of code by comparing expected and actual results.
assertEquals Method Syntax
Here is the basic syntax of the assertEquals
method:
assertEquals(expected, actual);
assertEquals(message, expected, actual);
assertEquals(expected, actual, delta);
assertEquals(message, expected, actual, delta);
Parameters:
expected
: The expected value.actual
: The actual value produced by the code.message
: Optional. A custom message to display if the assertion fails.delta
: Used for comparing floating-point numbers to account for precision errors.
Returns:
- Nothing. The method throws an assertion error if the values are not equal.
Examples
Basic Usage
Compare two integers.
Example
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
@Test
void testAddition() {
int expected = 5;
int actual = 2 + 3;
assertEquals(expected, actual);
}
}
Comparing Strings
Compare two strings.
Example
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class StringTest {
@Test
void testStringEquality() {
String expected = "Hello";
String actual = "Hello";
assertEquals(expected, actual);
}
}
Comparing Arrays
Compare two arrays using assertArrayEquals
.
Example
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import org.junit.jupiter.api.Test;
public class ArrayTest {
@Test
void testArrayEquality() {
int[] expected = {1, 2, 3};
int[] actual = {1, 2, 3};
assertArrayEquals(expected, actual);
}
}
Providing a Custom Message
Include a custom message to display if the assertion fails.
Example
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CustomMessageTest {
@Test
void testWithCustomMessage() {
int expected = 5;
int actual = 2 + 2;
assertEquals(expected, actual, "Custom Message: Expected value did not match actual value");
}
}
Handling Floating Point Numbers
Compare two floating-point numbers with a delta.
Example
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class FloatingPointTest {
@Test
void testFloatingPointEquality() {
double expected = 5.0;
double actual = 4.99;
assertEquals(expected, actual, 0.01);
}
}
Real-World Use Case
A common use case for assertEquals
is testing the methods of a Calculator
class to ensure they produce the correct results.
Create a Class Under Test – Calculator Class
The Calculator class will have basic arithmetic operations.
package org.example;
public class Calculator {
public int add(int a, int b) {
return a + b;
}
public int subtract(int a, int b) {
return a - b;
}
public double multiply(double a, double b) {
return a * b;
}
public double divide(double a, double b) {
if (b == 0) {
throw new IllegalArgumentException("Division by zero");
}
return a / b;
}
}
Create a Test Class – CalculatorTest class
package org.example;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
private final Calculator calculator = new Calculator();
@Test
void testAdd() {
assertEquals(5, calculator.add(2, 3));
}
@Test
void testSubtract() {
assertEquals(1, calculator.subtract(3, 2));
}
@Test
void testMultiply() {
assertEquals(6.0, calculator.multiply(2.0, 3.0), 0.001);
}
@Test
void testDivide() {
assertEquals(2.0, calculator.divide(6.0, 3.0), 0.001);
}
@Test
void testDivideByZero() {
try {
calculator.divide(1.0, 0);
} catch (IllegalArgumentException e) {
assertEquals("Division by zero", e.getMessage());
}
}
}
In this example, the CalculatorTest
class tests the Calculator
methods using assertEquals
. It includes tests for addition, subtraction, multiplication, division, and handling division by zero.
Output:
Conclusion
The assertEquals
method in JUnit is used for verifying that two values are equal in your tests. By using assertEquals
and its overloaded methods, you can compare various data types, provide custom messages for test failures, and handle floating-point precision issues.