AssertJ provides a wide range of assertions to verify the state of objects in a fluent and readable way. In this guide, we will see some of the most commonly used assertions.
Important AssertJ Assertions
Basic Assertions
isEqualTo()
: Asserts that the actual value is equal to the expected value.isNotEqualTo()
: Asserts that the actual value is not equal to the expected value.
String Assertions
contains()
: Asserts that the actual string contains the given sequence.doesNotContain()
: Asserts that the actual string does not contain the given sequence.startsWith()
: Asserts that the actual string starts with the given prefix.endsWith()
: Asserts that the actual string ends with the given suffix.isEmpty()
: Asserts that the actual string is empty.
Collection Assertions
hasSize()
: Asserts that the actual collection has the given size.contains()
: Asserts that the actual collection contains the given values.doesNotContain()
: Asserts that the actual collection does not contain the given values.containsExactly()
: Asserts that the actual collection contains exactly the given values in the given order.
Map Assertions
hasSize()
: Asserts that the actual map has the given size.containsKeys()
: Asserts that the actual map contains the given keys.containsValues()
: Asserts that the actual map contains the given values.doesNotContainKeys()
: Asserts that the actual map does not contain the given keys.doesNotContainValue()
: Asserts that the actual map does not contain the given value.
Object Assertions
isNotNull()
: Asserts that the actual object is not null.isNull()
: Asserts that the actual object is null.isInstanceOf()
: Asserts that the actual object is an instance of the given class.
Boolean Assertions
isTrue()
: Asserts that the actual boolean value is true.isFalse()
: Asserts that the actual boolean value is false.
Numeric Assertions
isGreaterThan()
: Asserts that the actual number is greater than the given number.isLessThan()
: Asserts that the actual number is less than the given number.isBetween()
: Asserts that the actual number is between the given start and end values (inclusive).
Date and Time Assertions
isBefore()
: Asserts that the actual date is before the given date.isAfter()
: Asserts that the actual date is after the given date.isEqualTo()
: Asserts that the actual date is equal to the given date.
Exception Assertions
isThrownBy()
: Asserts that the given exception is thrown by the provided code.
Soft Assertions
assertAll()
: Executes multiple assertions and reports all failures at the end.
BDD Assertions
then()
: BDD-style assertion using thethen
syntax for behavior-driven development.
AssertJ Assertions with Examples
1. Basic Assertions
isEqualTo()
Asserts that the actual value is equal to the expected value.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BasicAssertions {
@Test
public void testIsEqualTo() {
String name = "Anil";
assertThat(name).isEqualTo("Anil");
}
}
isNotEqualTo()
Asserts that the actual value is not equal to the expected value.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BasicAssertions {
@Test
public void testIsNotEqualTo() {
String name = "Anil";
assertThat(name).isNotEqualTo("Vikas");
}
}
2. String Assertions
contains()
Asserts that the actual string contains the given sequence.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringAssertions {
@Test
public void testContains() {
String sentence = "Anil is a software engineer";
assertThat(sentence).contains("software");
}
}
doesNotContain()
Asserts that the actual string does not contain the given sequence.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringAssertions {
@Test
public void testDoesNotContain() {
String sentence = "Anil is a software engineer";
assertThat(sentence).doesNotContain("doctor");
}
}
startsWith()
Asserts that the actual string starts with the given prefix.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringAssertions {
@Test
public void testStartsWith() {
String sentence = "Anil is a software engineer";
assertThat(sentence).startsWith("Anil");
}
}
endsWith()
Asserts that the actual string ends with the given suffix.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringAssertions {
@Test
public void testEndsWith() {
String sentence = "Anil is a software engineer";
assertThat(sentence).endsWith("engineer");
}
}
isEmpty()
Asserts that the actual string is empty.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class StringAssertions {
@Test
public void testIsEmpty() {
String emptyString = "";
assertThat(emptyString).isEmpty();
}
}
3. Collection Assertions
hasSize()
Asserts that the actual collection has the given size.
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class CollectionAssertions {
@Test
public void testHasSize() {
List<String> names = List.of("Arjun", "Priya", "Lakshmi");
assertThat(names).hasSize(3);
}
}
contains()
Asserts that the actual collection contains the given values.
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class CollectionAssertions {
@Test
public void testContains() {
List<String> names = List.of("Arjun", "Priya", "Lakshmi");
assertThat(names).contains("Priya", "Lakshmi");
}
}
doesNotContain()
Asserts that the actual collection does not contain the given values.
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class CollectionAssertions {
@Test
public void testDoesNotContain() {
List<String> names = List.of("Arjun", "Priya", "Lakshmi");
assertThat(names).doesNotContain("Ravi");
}
}
containsExactly()
Asserts that the actual collection contains exactly the given values in the given order.
import org.junit.jupiter.api.Test;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class CollectionAssertions {
@Test
public void testContainsExactly() {
List<String> names = List.of("Arjun", "Priya", "Lakshmi");
assertThat(names).containsExactly("Arjun", "Priya", "Lakshmi");
}
}
4. Map Assertions
hasSize()
Asserts that the actual map has the given size.
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class MapAssertions {
@Test
public void testHasSize() {
Map<String, Integer> ageMap = Map.of("Rohit", 28, "Sneha", 25, "Vikas", 35);
assertThat(ageMap).hasSize(3);
}
}
containsKeys()
Asserts that the actual map contains the given keys.
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class MapAssertions {
@Test
public void testContainsKeys() {
Map<String, Integer> ageMap = Map.of("Rohit", 28, "Sneha", 25, "Vikas", 35);
assertThat(ageMap).containsKeys("Rohit", "Sneha");
}
}
containsValues()
Asserts that the actual map contains the given values.
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class MapAssertions {
@Test
public void testContainsValues() {
Map<String, Integer> ageMap = Map.of("Rohit", 28, "Sneha", 25, "Vikas", 35);
assertThat(ageMap).containsValues(25, 35);
}
}
doesNotContainKeys()
Asserts that the actual map does not contain the given keys.
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class MapAssertions {
@Test
public void testDoesNotContainKeys() {
Map<String, Integer> ageMap = Map.of("Rohit", 28, "Sneha", 25, "Vikas", 35);
assertThat(ageMap).doesNotContainKeys("Amit");
}
}
doesNotContainValue()
Asserts that the actual map does not contain the given value.
import org.junit.jupiter.api.Test;
import java.util.Map;
import static org.assertj.core.api.Assertions.assertThat;
public class MapAssertions {
@Test
public void testDoesNotContainValue() {
Map<String, Integer> ageMap = Map.of("Rohit", 28, "Sneha", 25, "Vikas", 35);
assertThat(ageMap).doesNotContainValue(40);
}
}
5. Object Assertions
isNotNull()
Asserts that the actual object is not null.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ObjectAssertions {
@Test
public void testIsNotNull() {
String name = "Anil";
assertThat(name).isNotNull();
}
}
isNull()
Asserts that the actual object is null.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ObjectAssertions {
@Test
public void testIsNull() {
String name = null;
assertThat(name).isNull();
}
}
isInstanceOf()
Asserts that the actual object is an instance of the given class.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class ObjectAssertions {
@Test
public void testIsInstanceOf() {
String name = "Anil";
assertThat(name).isInstanceOf(String.class);
}
}
6. Boolean Assertions
isTrue()
Asserts that the actual boolean value is true.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BooleanAssertions {
@Test
public void testIsTrue() {
boolean isActive = true;
assertThat(isActive).isTrue();
}
}
isFalse()
Asserts that the actual boolean value is false.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class BooleanAssertions {
@Test
public void testIsFalse() {
boolean isActive = false;
assertThat(isActive).isFalse();
}
}
7. Numeric Assertions
isGreaterThan()
Asserts that the actual number is greater than the given number.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class NumericAssertions {
@Test
public void testIsGreaterThan() {
int age = 30;
assertThat(age).isGreaterThan(20);
}
}
isLessThan()
Asserts that the actual number is less than the given number.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class NumericAssertions {
@Test
public void testIsLessThan() {
int age = 30;
assertThat(age).isLessThan(40);
}
}
isBetween()
Asserts that the actual number is between the given start and end values (inclusive).
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThat;
public class NumericAssertions {
@Test
public void testIsBetween() {
int age = 30;
assertThat(age).isBetween(20, 40);
}
}
8. Date and Time Assertions
isBefore()
Asserts that the actual date is before the given date.
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import static org.assertj.core.api.Assertions.assertThat;
public class DateAssertions {
@Test
public void testIsBefore() {
LocalDate today = LocalDate.now();
LocalDate tomorrow = today.plusDays(1);
assertThat(today).isBefore(tomorrow);
}
}
isAfter()
Asserts that the actual date is after the given date.
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import static org.assertj.core.api.Assertions.assertThat;
public class DateAssertions {
@Test
public void testIsAfter() {
LocalDate today = LocalDate.now();
LocalDate yesterday = today.minusDays(1);
assertThat(today).isAfter(yesterday);
}
}
isEqualTo()
Asserts that the actual date is equal to the given date.
import org.junit.jupiter.api.Test;
import java.time.LocalDate;
import static org.assertj.core.api.Assertions.assertThat;
public class DateAssertions {
@Test
public void testIsEqualTo() {
LocalDate today = LocalDate.now();
assertThat(today).isEqualTo(LocalDate.now());
}
}
9. Exception Assertions
isThrownBy()
Asserts that the given exception is thrown by the provided code.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
public class ExceptionAssertions {
@Test
public void testIsThrownBy() {
assertThatThrownBy(() -> {
throw new IllegalArgumentException("Invalid argument");
}).isInstanceOf(IllegalArgumentException.class)
.hasMessage("Invalid argument");
}
}
10. Soft Assertions
Soft assertions allow multiple assertions to be executed, collecting all failures to be reported at the end.
import org.assertj.core.api.SoftAssertions;
import org.junit.jupiter.api.Test;
public class SoftAssertionsExample {
@Test
public void testSoftAssertions() {
SoftAssertions softAssertions = new SoftAssertions();
softAssertions.assertThat("Amit").isEqualTo("Amit");
softAssertions.assertThat(25).isGreaterThan(20);
softAssertions.assertThat(false).isTrue(); // This will fail
softAssertions.assertThat("Lakshmi").isNotEmpty();
softAssertions.assertAll(); // This will report all failures
}
}
Output
SoftAssertionsExample.testSoftAssertions:
1) expected:<[tru]e> but was:<[fals]e>
11. BDD Assertions
BDD-style assertions using the then
syntax for behavior-driven development.
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.BDDAssertions.then;
public class BddAssertionsExample {
@Test
public void testBddAssertions() {
String name = "Lakshmi";
int age = 27;
then(name).isEqualTo("Lakshmi");
then(age).isBetween(25, 30);
}
}
Output
Test passed
Conclusion
AssertJ provides a rich set of assertions for various types of objects, collections, maps, dates, exceptions, and more. The fluent API and extensive assertions improve the readability and maintainability of tests. This section covered the most important AssertJ assertions, including basic, string, collection, map, object, boolean, numeric, date, exception, soft, and BDD assertions, with examples to illustrate their usage. For more detailed information and advanced features, refer to the official AssertJ documentation.
Comments
Post a Comment
Leave Comment