Assertions class provides various methods for testing equality.
public static void assertEquals(short expected, short actual)
public static void assertEquals(short expected, short actual, String message)
public static void assertEquals(short expected, short actual, Supplier<String> messageSupplier)
public static void assertEquals(byte expected, byte actual)
public static void assertEquals(byte expected, byte actual, String message)
public static void assertEquals(byte expected, byte actual, Supplier<String> messageSupplier)
public static void assertEquals(int expected, int actual)
public static void assertEquals(int expected, int actual, String message)
public static void assertEquals(int expected, int actual, Supplier<String> messageSupplier)
public static void assertEquals(long expected, long actual)
public static void assertEquals(long expected, long actual, String message)
public static void assertEquals(long expected, long actual, Supplier<String> messageSupplier)
public static void assertEquals(char expected, char actual)
public static void assertEquals(char expected, char actual, String message)
public static void assertEquals(char expected, char actual, Supplier<String> messageSupplier)
public static void assertEquals(float expected, float actual)
public static void assertEquals(float expected, float actual, String message)
public static void assertEquals(float expected, float actual, Supplier<String> messageSupplier)
public static void assertEquals(float expected, float actual, float delta)
public static void assertEquals(float expected, float actual, float delta, String message)
public static void assertEquals(float expected, float actual, float delta, Supplier<String> messageSupplier)
public static void assertEquals(double expected, double actual)
public static void assertEquals(double expected, double actual, String message)
public static void assertEquals(double expected, double actual, Supplier<String> messageSupplier)
public static void assertEquals(double expected, double actual, double delta)
public static void assertEquals(double expected, double actual, double delta, String message)
public static void assertEquals(double expected, double actual, double delta, Supplier<String> messageSupplier)
public static void assertEquals(Object expected, Object actual)
public static void assertEquals(Object expected, Object actual, String message)
public static void assertEquals(Object expected, Object actual, Supplier<String> messageSupplier)
Each of the above methods comes with versions of assertNotEqual(....)
Starting version 5.4.0, JUnit Jupiter began supporting number wrappers (e.g., Short, Character, Byte, Integer, Long, Float, and Double) in assertEquals and assertNotEquals methods.
Examples
Equality Examples
package com.logicbig.example;
import org.junit.jupiter.api.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class EqualityExampleTest {
@Test
void testIntegerEquality() {
int expected = 5;
int actual = 2 + 3;
assertEquals(expected, actual);
}
@Test
void testStringEquality() {
String expected = "Hello World";
String actual = "Hello " + "World";
assertEquals(expected, actual);
}
@Test
void testDoubleEquality() {
double expected = 3.14;
double actual = Math.PI;
// Using delta for floating-point comparison
assertEquals(expected, actual, 0.01,
"Input value must be equal to Math.PI within 0.01");
}
@Test
void testBooleanEquality() {
boolean expected = true;
boolean actual = (10 > 5);
assertEquals(expected, actual);
}
}
mvn test -Dtest=EqualityExampleTest OutputD:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions>mvn test -Dtest=EqualityExampleTest [INFO] Scanning for projects... [INFO] [INFO] ----------< com.logicbig.example:junit-5-equality-assertions >---------- [INFO] Building junit-5-equality-assertions 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-equality-assertions --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-equality-assertions --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-equality-assertions --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-equality-assertions --- [INFO] Recompiling the module because of added or removed source files. [INFO] Compiling 3 source files with javac [debug target 25] to target\test-classes [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-equality-assertions --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.EqualityExampleTest [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.121 s -- in com.logicbig.example.EqualityExampleTest [INFO] [INFO] Results: [INFO] [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 3.626 s [INFO] Finished at: 2025-12-01T10:54:56+08:00 [INFO] ------------------------------------------------------------------------
assertNotEquals Examples
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
public class BasicAssertNotEqualsExamples {
@Test
void testIntegerInequality() {
int expected = 5;
int actual = 2 + 4;
assertNotEquals(expected, actual);
}
@Test
void testStringInequality() {
String expected = "Hello World";
String actual = "Hello Java";
assertNotEquals(expected, actual);
}
@Test
void testBooleanInequality() {
boolean expected = true;
boolean actual = (10 < 5);
assertNotEquals(expected, actual);
}
@Test
void testNullComparison() {
String expected = "Some value";
String actual = null;
assertNotEquals(expected, actual);
}
}
mvn test -Dtest=BasicAssertNotEqualsExamples OutputD:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions>mvn test -Dtest=BasicAssertNotEqualsExamples [INFO] Scanning for projects... [INFO] [INFO] ----------< com.logicbig.example:junit-5-equality-assertions >---------- [INFO] Building junit-5-equality-assertions 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-equality-assertions --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-equality-assertions --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-equality-assertions --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-equality-assertions --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-equality-assertions --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.BasicAssertNotEqualsExamples [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.076 s -- in com.logicbig.example.BasicAssertNotEqualsExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 4, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.970 s [INFO] Finished at: 2025-12-01T10:55:02+08:00 [INFO] ------------------------------------------------------------------------
Object Comparison Example
package com.logicbig.example;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
public class ObjectComparisonExamples {
@Test
void testObjectEquality() {
Person expected = new Person("John", 25);
Person actual = new Person("John", 25);
assertEquals(expected, actual);
}
class Person {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
// equals and hashCode methods must be implemented for proper comparison
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null || getClass() != obj.getClass())
return false;
Person person = (Person) obj;
return age == person.age && name.equals(person.name);
}
@Override
public int hashCode() {
return java.util.Objects.hash(name, age);
}
}
}
mvn test -Dtest=ObjectComparisonExamples OutputD:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions>mvn test -Dtest=ObjectComparisonExamples [INFO] Scanning for projects... [INFO] [INFO] ----------< com.logicbig.example:junit-5-equality-assertions >---------- [INFO] Building junit-5-equality-assertions 1.0-SNAPSHOT [INFO] from pom.xml [INFO] --------------------------------[ jar ]--------------------------------- [INFO] [INFO] --- resources:3.3.1:resources (default-resources) @ junit-5-equality-assertions --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions\src\main\resources [INFO] [INFO] --- compiler:3.14.1:compile (default-compile) @ junit-5-equality-assertions --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- resources:3.3.1:testResources (default-testResources) @ junit-5-equality-assertions --- [WARNING] Using platform encoding (UTF-8 actually) to copy filtered resources, i.e. build is platform dependent! [INFO] skip non existing resourceDirectory D:\example-projects\junit-5\junit-5-assertions\junit-5-equality-assertions\src\test\resources [INFO] [INFO] --- compiler:3.14.1:testCompile (default-testCompile) @ junit-5-equality-assertions --- [INFO] Nothing to compile - all classes are up to date. [INFO] [INFO] --- surefire:3.2.5:test (default-test) @ junit-5-equality-assertions --- [INFO] Using auto detected provider org.apache.maven.surefire.junitplatform.JUnitPlatformProvider [INFO] [INFO] ------------------------------------------------------- [INFO] T E S T S [INFO] ------------------------------------------------------- [INFO] Running com.logicbig.example.ObjectComparisonExamples [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0, Time elapsed: 0.051 s -- in com.logicbig.example.ObjectComparisonExamples [INFO] [INFO] Results: [INFO] [INFO] Tests run: 1, Failures: 0, Errors: 0, Skipped: 0 [INFO] [INFO] ------------------------------------------------------------------------ [INFO] BUILD SUCCESS [INFO] ------------------------------------------------------------------------ [INFO] Total time: 2.796 s [INFO] Finished at: 2025-12-01T10:55:06+08:00 [INFO] ------------------------------------------------------------------------
Example ProjectDependencies and Technologies Used: - junit-jupiter-engine 6.0.1 (Module "junit-jupiter-engine" of JUnit)
Version Compatibility: 5.0.0 - 6.0.1 Version compatibilities of junit-jupiter-engine with this example:
- 5.0.0
- 5.0.1
- 5.0.2
- 5.0.3
- 5.1.0
- 5.1.1
- 5.2.0
- 5.3.0
- 5.3.1
- 5.3.2
- 5.4.0
- 5.4.1
- 5.4.2
- 5.5.0
- 5.5.1
- 5.5.2
- 5.6.0
- 5.6.1
- 5.6.2
- 5.6.3
- 5.7.0
- 5.7.1
- 5.7.2
- 5.8.0
- 5.8.1
- 5.8.2
- 5.9.0
- 5.9.1
- 5.9.2
- 5.9.3
- 5.10.0
- 5.10.1
- 5.10.2
- 5.10.3
- 5.10.4
- 5.10.5
- 5.11.0
- 5.11.1
- 5.11.2
- 5.11.3
- 5.11.4
- 5.12.0
- 5.12.1
- 5.12.2
- 5.13.0
- 5.13.1
- 5.13.2
- 5.13.3
- 5.13.4
- 5.14.0
- 5.14.1
- 6.0.0
- 6.0.1
Versions in green have been tested.
- JDK 25
- Maven 3.9.11
|
|