The assertNotEquals
method in JUnit is used to test if two values are not 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 assertNotEquals
method, including its syntax and examples of its different overloads.
Table of Contents
- Introduction
assertNotEquals
Method Syntax- Examples
- Basic Usage
- Comparing Strings
- Comparing Arrays
- Providing a Custom Message
- Handling Floating Point Numbers
- Real-World Use Case
- Conclusion
Introduction
The assertNotEquals
method in JUnit is an assertion method used to verify that two values are not equal. If the values are 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 ensuring that certain values are not the same.
assertNotEquals Method Syntax
Here is the basic syntax of the assertNotEquals
method:
assertNotEquals(expected, actual);
assertNotEquals(message, expected, actual);
assertNotEquals(expected, actual, delta);
assertNotEquals(message, expected, actual, delta);
Parameters:
expected
: The value that is expected to be different from the actual 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 equal.
Examples
Basic Usage
Compare two integers to ensure they are not equal.
Example
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class CalculatorTest {
@Test
void testAddition() {
int unexpected = 4;
int actual = 2 + 3;
assertNotEquals(unexpected, actual);
}
}
Comparing Strings
Compare two strings to ensure they are not equal.
Example
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class StringTest {
@Test
void testStringInequality() {
String unexpected = "Hello";
String actual = "World";
assertNotEquals(unexpected, actual);
}
}
Comparing Arrays
To compare two arrays, use assertNotEquals
indirectly by comparing their string representations.
Example
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
public class ArrayTest {
@Test
void testArrayInequality() {
int[] unexpected = {1, 2, 3};
int[] actual = {4, 5, 6};
assertNotEquals(Arrays.toString(unexpected), Arrays.toString(actual));
}
}
Providing a Custom Message
Include a custom message to display if the assertion fails.
Example
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class CustomMessageTest {
@Test
void testWithCustomMessage() {
int unexpected = 5;
int actual = 2 + 2;
assertNotEquals(unexpected, actual, "Custom Message: Unexpected value matched actual value");
}
}
Handling Floating Point Numbers
Compare two floating-point numbers with a delta to account for precision errors.
Example
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class FloatingPointTest {
@Test
void testFloatingPointInequality() {
double unexpected = 5.0;
double actual = 4.99;
assertNotEquals(unexpected, actual, 0.001);
}
}
Real-World Use Case
Testing a BookService Class
A common use case for assertNotEquals
is testing the methods of a BookService
class to ensure that certain operations do not produce the same results.
Class Under Test
import java.util.HashMap;
import java.util.Map;
public class BookService {
private Map<String, String> books = new HashMap<>();
public boolean addBook(String isbn, String title) {
if (books.containsKey(isbn)) {
return false;
}
books.put(isbn, title);
return true;
}
public boolean updateBook(String isbn, String title) {
if (!books.containsKey(isbn)) {
return false;
}
books.put(isbn, title);
return true;
}
public boolean deleteBook(String isbn) {
if (!books.containsKey(isbn)) {
return false;
}
books.remove(isbn);
return true;
}
public String getBook(String isbn) {
return books.get(isbn);
}
}
Test Class
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class BookServiceTest {
private final BookService bookService = new BookService();
@Test
void testAddBook() {
bookService.addBook("123456", "JUnit 5 Guide");
assertNotEquals("Different Book", bookService.getBook("123456"));
}
@Test
void testUpdateBook() {
bookService.addBook("123456", "JUnit 5 Guide");
bookService.updateBook("123456", "JUnit 5 Advanced Guide");
assertNotEquals("JUnit 5 Guide", bookService.getBook("123456"));
}
@Test
void testDeleteBook() {
bookService.addBook("123456", "JUnit 5 Guide");
bookService.deleteBook("123456");
assertNotEquals("JUnit 5 Guide", bookService.getBook("123456"));
}
}
In this example, the BookServiceTest
class tests the BookService
methods using assertNotEquals
. It includes tests for adding, updating, and deleting books to ensure that the results are not the same when expected.
Conclusion
The assertNotEquals
method in JUnit is used for verifying that two values are not equal in your tests. By using assertNotEquals
and its overloaded methods, you can compare various data types, provide custom messages for test failures, and handle floating-point precision issues. Understanding and using the assertNotEquals
method effectively is crucial for developing robust and maintainable Java applications.