The org.springframework.util.Assert class in Spring is a small but powerful utility used to validate method arguments and state at runtime. It helps you fail fast by throwing an IllegalArgumentException (or IllegalStateException) when a condition is not met π¨
Itβs mainly used for:
- Validating constructor arguments
- Checking method parameters
- Ensuring application state correctness
It is similar in concept to Objects.requireNonNull() but much more expressive.
π Why Use Assert?
Instead of writing:
if (user == null) {
throw new IllegalArgumentException("User must not be null");
}
You can write:
Assert.notNull(user, "User must not be null");
Cleaner, readable, and consistent across the codebase β¨
πΉ Commonly Used Methods in Assert
1οΈβ£ notNull()
Ensures an object is not null.
import org.springframework.util.Assert;
public class UserService {
public void processUser(String username) {
Assert.notNull(username, "Username must not be null");
System.out.println("Processing: " + username);
}
}
If username is null β throws IllegalArgumentException.
2οΈβ£ hasText()
Checks that a String contains non-whitespace text.
Assert.hasText(name, "Name must not be empty");
Fails for:
null""" "
Very useful in controllers or service validation.
3οΈβ£ hasLength()
Checks if a string is not null and not empty (but allows whitespace).
Assert.hasLength(password, "Password must not be empty");
4οΈβ£ isTrue()
Validates a boolean expression.
Assert.isTrue(age >= 18, "Age must be at least 18");
If false β throws IllegalArgumentException.
5οΈβ£ notEmpty()
For collections, maps, or arrays.
Assert.notEmpty(users, "User list must not be empty");
Works for:
- Collection
- Map
- Array
6οΈβ£ noNullElements()
Ensures collection/array contains no null values.
Assert.noNullElements(userArray, "User array must not contain null elements");
7οΈβ£ state()
Used to validate object state (throws IllegalStateException instead).
Assert.state(connection.isOpen(), "Connection must be open");
Difference:
isTrue()β IllegalArgumentExceptionstate()β IllegalStateException
π Real Spring Example (Constructor Validation)
import org.springframework.stereotype.Service;
import org.springframework.util.Assert;
@Service
public class PaymentService {
private final String provider;
public PaymentService(String provider) {
Assert.hasText(provider, "Provider must not be empty");
this.provider = provider;
}
}
This ensures the bean is never created with invalid data β‘
π§ When Should You Use It?
β
In service layer
β
In constructors
β
In configuration classes
β Not for user input validation in controllers (use Bean Validation instead: @NotNull, @NotBlank)
βοΈ Assert vs Bean Validation
| Feature | Assert | Bean Validation |
|---|---|---|
| Purpose | Developer checks | User input validation |
| Exception | Runtime exception | Validation errors |
| Layer | Service/internal | Controller/DTO |
π― Best Practice
Use Assert for defensive programming inside your application logic.
Use @Valid + @NotNull for request validation.
π Summary
The Spring Assert class:
- Helps validate arguments quickly
- Improves readability
- Encourages fail-fast design
- Reduces boilerplate
ifchecks
Itβs small, simple, and very useful in clean Spring applications π‘